Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 07-02-2010, 12:03 PM   #1
skunkchop
Registered User
 
Join Date: Jul 2010
Posts: 3
Default [AS2] collision problems

I'm new to this forum and I feel like I'll be staying.
Don't laugh at how simple my problems are kthx.

My first problem is that I use
ActionScript Code:
if (_root.wall.hitTest(_root.Hero)) { } else
to check if there is NO collision.
It seems unnecessary to do this, when I should be able to do something like:
ActionScript Code:
if NOT (_root.wall.hitTest(_root.Hero))
I just want to know how to check for NO collision in a better way.


My second problem is that I don't know how to make one piece of code apply to more than one object. Let's say I want walls in many places. I should be able to make the code for the wall only once, and put it in many places, right? For some reason, the collision code only works for one of the walls I use, and I go right through the rest. I tried copy and pasting more walls, and I tried dragging more walls from the library.


And a third question(not really problem) is what is _root? I read the definition and I don't understand it. Why is it necessary to put _root.wall instead of just wall?

Any help is greatly appreciated.

EDIT: Sorry, I just noticed the newbie section. I'm going there now.

Last edited by skunkchop; 07-02-2010 at 12:09 PM.
skunkchop is offline   Reply With Quote
Old 07-02-2010, 12:11 PM   #2
skunkchop
Registered User
 
Join Date: Jul 2010
Posts: 3
Default I'm new and have a few simple problems

Hey, I'm new.

My first problem is that I use
ActionScript Code:
if (_root.wall.hitTest(_root.Hero)) { } else
to check if there is NO collision.
It seems unnecessary to do this, when I should be able to do something like:
ActionScript Code:
if NOT (_root.wall.hitTest(_root.Hero))
I just want to know how to check for NO collision in a better way.


My second problem is that I don't know how to make one piece of code apply to more than one object. Let's say I want walls in many places. I should be able to make the code for the wall only once, and put it in many places, right? For some reason, the collision code only works for one of the walls I use, and I go right through the rest. I tried copy and pasting more walls, and I tried dragging more walls from the library.


And a third question(not really problem) is what is _root? I read the definition and I don't understand it. Why is it necessary to put _root.wall instead of just wall?

Any help is greatly appreciated.
skunkchop is offline   Reply With Quote
Old 07-02-2010, 01:06 PM   #3
kadirkhan
Flash Developer
 
Join Date: Nov 2007
Location: New Delhi, India
Posts: 40
Default

Your first problem is not a problem. Your program is alright to check NOT Hit.

According to me there is no other option simpler than yours.


Your second problem:
You have to make a single function and call it for all. Or you may make a single movie clip for wall. Or You may make several mc like wall1, wall2 etc.

Your third problem is very simple. I am not going to define _root. If you are on stage and you have defined some instance name directly on it and you are calling it from the stage (Not under the movie clip or scene). You not need to write _root. second condition: If your instance name or variable has been declared on stage directly and you are calling it under a mc which is on the stage. you need to write

ActionScript Code:
_root.instancename
For example: on the very first frame you declare a variable var abc. and you want to trace it from stage. just write

ActionScript Code:
trace(abc).

If you are working under a movie clip and want to trace abc then write

ActionScript Code:
trace(_root.abc);
kadirkhan is offline   Reply With Quote
Old 07-02-2010, 03:30 PM   #4
neilmmm
Senior Member
 
neilmmm's Avatar
 
Join Date: Oct 2005
Location: dorset
Posts: 1,637
Default

Code:
if (!_root.wall.hitTest(_root.Hero))
if not hitTesting
neilmmm is offline   Reply With Quote
Old 07-03-2010, 12:58 AM   #5
skunkchop
Registered User
 
Join Date: Jul 2010
Posts: 3
Default

Thanks! My first problem is definitely solved. However, I don't really understand the solution to my second problem. I'm not very good with Flash terminology right now.
Let's say I want to put many platforms to jump on. The collision code only works for ONE of those platforms, I go right through the other ones.


And I have a new problem too.
When I collide with a wall, I get stuck inside the wall. Here's my code:
ActionScript Code:
onClipEvent(load) { power = 1; uppower = 1 xspeed = 0; yspeed = 0; } onClipEvent(enterFrame) { if (!_root.wall.hitTest(_root.Hero))     { if (Key.isDown(Key.LEFT))         xspeed -= power     if(Key.isDown(Key.RIGHT))         xspeed += power     if(Key.isDown(Key.UP))         yspeed -= uppower     if(Key.isDown(Key.DOWN))         yspeed += uppower     } _x += xspeed _y += yspeed if (_root.wall.hitTest(_root.Hero)) {         xspeed = 0;         yspeed = 0; } }
skunkchop is offline   Reply With Quote
Old 07-03-2010, 04:38 AM   #6
kadirkhan
Flash Developer
 
Join Date: Nov 2007
Location: New Delhi, India
Posts: 40
Default

JUST TRY THIS ONE CODE:

ActionScript Code:
onClipEvent(load) { power = 1; uppower = 1 xspeed = 0; yspeed = 0; } onClipEvent(enterFrame) { if (!_root.wall.hitTest(_root.Hero))     { if (Key.isDown(Key.LEFT))         xspeed -= power     if(Key.isDown(Key.RIGHT))         xspeed += power     if(Key.isDown(Key.UP))         yspeed -= uppower     if(Key.isDown(Key.DOWN))         yspeed += uppower     } _x += xspeed _y += yspeed } if (_root.wall.hitTest(_root.Hero)) {         xspeed = 0;         yspeed = 0; }
[/QUOTE]

PROBLEM: Your second if will never going to be TRUE; becoz it is under first if CONDITION. Hope it will work.
kadirkhan is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 06:17 AM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.