Lior Yang Eliav
Lior Yang Eliav

Reputation: 25

brick breaker hit test error

okay heres the thing, Im making a brick breaker game. im trying to build it so that i can add the bricks on the stage by hand instead of using a code.

on the stage a have a ball ("Ball") that for the moment is folowing the mouse movement, and a brick movieclip with a hit test code inside.

heres the mainframe code:

import flash.events.Event;

addEventListener(Event.ENTER_FRAME,moveBall);

function moveBall (e:Event):void{
Ball.x = mouseX;
Ball.y = mouseY;
}

and heres the code that i wrote inside the brick movieclip:

addEventListener(Event.ENTER_FRAME,checkHit);

function checkHit (e:Event):void{
    if(this.hitTestObject(Ball)){
    trace ("HIT!!");
}
}

thats all the code and the ball and brick are alredy on the stage. when i ran this i get "Symbol 'brick', Layer 'Layer 1', Frame 1, Line 6 1120: Access of undefined property Ball."

line 6 is:

if(this.hitTestObject(Ball)){

please tell me why and how to fix this.. i tried to change the object to stage.Ball and I still get an error :(

thanks in advance

Upvotes: 1

Views: 304

Answers (1)

BadFeelingAboutThis
BadFeelingAboutThis

Reputation: 14406

You receive the error because Ball is not in the scope of any of your bricks (it's a different timeline that doesn't know about Ball).

you could access the Ball by using the parent keyword. eg: MovieClip(parent).Ball would probably work.

A suggestion that would be more efficient: change this addEventListener(Event.ENTER_FRAME,moveBall); to use MouseEvent.MOUSE_MOVE instead of ENTER_FRAME. That way it only fires if the mouse is moving.

Upvotes: 1

Related Questions