0
votes

How do I get my hit test object to work, the picture is:

One Object in another class should sense when an object from a different class is touching it via hitTestObject.

ActionScript:

package  {

import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;


public class Grey extends Sprite {
    var blue:Blue = new Blue();
    private var changes:Boolean
    private var directions
    private var speed:int = 20;
    public function Grey() {
        // constructor code
        stage.addEventListener(Event.ENTER_FRAME, loop)
        /*stage.addEventListener(KeyboardEvent.KEY_DOWN, pressed)*/
    }
    function loop(e:Event){
        movements();
        hitTesting();
    }
    function movements(){
        if (changes==true){
            directions = -1;
        }else{
            directions = 1;
        }
    x+=speed * directions
    }
    function hitTesting(){
        if (this.hitTestObject(blues)){
            changes=true
        }
    }
}

}

1

1 Answers

0
votes

Just did this a few days ago for a game I'm working on :D

For one thing, you don't have blues declared anywhere. What you want to do is to store all of your Blue objects into an array like so:

var blues:Array = new Array();

Then whenever you create the array, make sure you put it in:

var blue:Blue = new Blue();
blues.push(blue); //yay variable names!

Finally, in your EnterFrame function (or hitTesting, since you call that in your on frame function):

for each(var blue:Blue in blues){
    if (this.hitTestObject(blue)){
        changes = true; //or whatever functionality. I use a contact function in the object hit
    }
}

Edit: One thing to note: your Blue class has NO idea that the grey is touching it. The way you have your code set up, it looks as if grey is aware of blue, but blue doesn't care one way or another. Just wanted to make sure that was clear because of the way you asked the question.