Sign in to follow this  
robertosaget

flash collision detection help. It is really basic

Recommended Posts

Hello, I am currently learning the basics of flash and i am on collision detection. I am trying to test to see if two rectangular objects are hitting and if they are i want the first rectangle(The one that is moving) to stop. Here is a snippet of my code, i know something basic is not going on but i cant see what. addEventListener(Event.ENTER_FRAME, hitTest); function hitTest(e:Event):void { if(robot_b.hitTestObject(colbox_b)) { } else { addEventListener(Event.ENTER_FRAME ,moverobot); } } robot_b is the moving object and it it a symbol and colbox_b is a symbol and it is stationary. What happens is robot_b still moves even when they are hit. I am using cs4 What should i do? Should i post the rest of the code or will this suffice? Thanks

Share this post


Link to post
Share on other sites
I'm not very experienced with actionscript, but it looks to me that for every frame there is no collision, you are adding another event listener which listens for the ENTER_FRAME event, which calls the moverobot method when the ENTER_FRAME event fires.

I'm guessing you want this event listener to be removed when a collision occurs, so this should be in you if block. You only want one event listener to be added when no collision is occuring, not one every frame as looks to be hapenning at the moment.


A better idea may be to work with velocities instead of adding / removing event listeners to move your character.

Something like :



addEventListener(Event.ENTER_FRAME, update);


function Update(event:Event):void
{

var xVelocity:Float = 0.0f;
var yVelocity:Float = 0.0f;


if(KeyBoard.KeyDown.Left)
xVelocity = -5;

if(KeyBoard.KeyDown.Right)
xVelocity = 5;

if(KeyBoard.KeyDown.Up)
yVelocity = -5;

if(KeyBoard.KeyDown.Down)
yVelocity = 5;

var oldPos:Point = Point(robot_b.X, robot_b.Y);

robot_b.X += xVelocity;
robot_b.Y += yVelocity;

if(robot_b.hitTestObject(coolbox_b))
{
robot_b.X = oldPos.X;
robot_b.Y = oldPos.Y;
}

}





The code above isn't real actionscript, I just made up the objects to demonstrate the idea, so it isn't going to compile. Hopefully I got the general idea across. Unless I missed the mark completely, I don't have any experience in the language so maybe your code is the way it should be done, in which case you can just ignore me!

Good luck.

Share this post


Link to post
Share on other sites
Thank you. The implementation makes sense but my execution is lacking. I implemented it with a few minor tweaks to it like you said i should do and i get this error

Error: Error #2090: The Proxy class does not implement callProperty. It must be overridden by a subclass.
at Error$/throwError()
at flash.utils::Proxy/http://www.adobe.com/2006/actionscript/flash/proxy::callProperty()
at game1_fla::MainTimeline/update()

i have never seen this error before and i am a bit confused. I went to the website but that did not help because it sent me to an error page. What should i do?
Thanks

Share this post


Link to post
Share on other sites
Again, I'm not a flash user, but looking at the error message - maybe you made the same mistake I did in my code above - when adding the event listener I asked it to callback a function called update, however I never implemented an update function. I did implement an Update function (note the uppercase 'U').

Make sure the function name you pass the addEventListener function is the same name as the function you implement to be called back.


Share this post


Link to post
Share on other sites
Ported DaveMS's pseudo to actionscript. haven't tested it though but it should give you a general idea.


import flash.geom.Point;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;

var leftPressed:Boolean;
var rightPressed:Boolean;
var upPressed:Boolean;
var downPressed:Boolean;


addEventListener(Event.ENTER_FRAME, update);
addEventListener( KeyboardEvent.KEY_DOWN, onKeyDown );
addEventListener( KeyboardEvent.KEY_UP, onKeyUp );

function onKeyDown( evt:KeyboardEvent ):void
{
switch( e.keycode)
{
case Keyboard.LEFT: leftPressed = true; break;
case Keyboard.RIGHT: rightPressed = true; break;
case Keyboard.UP: upPressed = true; break;
case Keyboard.DOWN: downPressed = true; break;
}
}

function onKeyUp( evt:KeyboardEvent ):void
{
switch( e.keycode)
{
case Keyboard.LEFT: leftPressed = false; break;
case Keyboard.RIGHT: rightPressed = false; break;
case Keyboard.UP: upPressed = false; break;
case Keyboard.DOWN: downPressed = false; break;
}
}

function update(event:Event):void
{

var xVelocity:Number = [0].[0];
var yVelocity:Number = [0].[0];


if(leftPressed)
xVelocity = -[5];

if(rightPressed)
xVelocity = [5];

if(upPressed)
yVelocity = -[5];

if(downPressed)
yVelocity = [5];

var oldPos:Point = new Point(robot_b.x, robot_b.y);

robot_b.x += xVelocity;
robot_b.y += yVelocity;

if(robot_b.hitTestObject(coolbox_b))
{
robot_b.x = oldPos.x;
robot_b.y = oldPos.y;
}
}

Share this post


Link to post
Share on other sites
Thanks egonvb but now i have another problem. For some reason hit detection with that function is not registering. The robot hits the box and he keeps on moving with ease. I know i am doing something wrong because i used your exact code and still i am having this issue.

Secondly i have an unrelated problem with the movie clips and symbols. When i press f8 on the rectangle it gives me the pop up window and i name the movie clip and export it to actionscript. I then give it an instance name in properties. When i run the code it says that it does not recognize robot and i have to rename it robot_b in the instance name and everywhere else to work. Why is that? The club leader said flash just does that but that seems very odd?
Thanks

Share this post


Link to post
Share on other sites
Hi robertosaget,

if you copied egonvb's code exactly, there are a few typos -

I think the switch statements in the onKeyUp and onKeyDown functions should read:



switch(evt.keycode)
{
...
}



The hitTest if statement should read:


if(robot_b.hitTestObject(colbox_b))



not


if(robot_b.hitTestObject(coolbox_b))



I'd imagine you could find these typos during compilation, so this probably isn't your problem. I think hitTestObject checks the collision boxes between objects, so as a last resort, check that your collision boxes are correct, I think if you select a symbol on the stage it will show the bounding box which is used for collisions. If all this is correct, then I'm out of ideas. Egonvb seems to have more experience than me at this so maybe he can help you some more.

Share this post


Link to post
Share on other sites
After the first time that a hit is NOT detected between the two, there is an event listener added to the frame. Once this has been added, it stays there. In order to stop it that way, you'd have to use some form of removeEventListener(), but that seems pretty tedious.

I came up with a pretty simple fix by just skipping the event listeners. It works, I've tested it. (which was fun because I got to learn a little bit about AS3)


addEventListener(Event.ENTER_FRAME, hitTest);

function hitTest(e:Event):void
{
if(!robot_b.hitTestObject(colbox_b))
{
moveRobot(robot_b);
}
}

function moveRobot(robot:Object):void
{
robot.x += 1;
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this