Question

I do not know how to add the john array and make a hittestobject with it. Bal is a different class non relevant to this problem. I've tried to do john[new Bal] tried john[ k ] tried z and to specify z as a for-loop but then i would just get Z balls place. This is supposed to become a space-invader type of game. I'm trying to make a hit test object between HENK and the 'falling balls' (JOHN). I do not know how to work with arrays especially given the fact that is should be timer-triggered.

Thanks

public class Main extends Sprite

{
   public var henk:Sprite = new Sprite();
   public var level:Timer = new Timer (2000, 0);
   public var valTijd:Number = new Number
   public var i:Number = 2000;
   public var john:Array = new Array(); 
   public var k:Number = 9000;  

   public function Main():void


   {
        henk.graphics.beginFill(0xFF00FF);
        henk.graphics.drawCircle(0, 500, 20);
        henk.graphics.endFill();
        addChild(henk);

        level.addEventListener(TimerEvent.TIMER, up);
        level.start();

        henk.addEventListener(Event.ENTER_FRAME, muis);
        henk.addEventListener(Event.ENTER_FRAME, hit); 


   }

    public function up(e:TimerEvent):void
    {             
        var tijdje:Timer = new Timer( i, 0)
        tijdje.addEventListener(TimerEvent.TIMER, tijdLuisteraar);
        tijdje.start();
         i = i - 250;

    }
       public function muis (e:Event):void
    {
        henk.x = mouseX;
    }






    public function hit (e:Event): void
    {
        if ( henk.hitTestObject(john [k] )) 
         { 
             if (contains(john[k] ))
            {
                removeChild(henk);
                    }

         }

    }



    public function tijdLuisteraar(e:TimerEvent):void
    {



                john.push(new Bal);
                addChild(john[k]);



    }

}

}

Was it helpful?

Solution

welcome to stackoverflow!

This problem is actually fairly simple, I will describe how you will probably want to use an array in the case you described.

At the part where you create new Balls you want to append them to an array, which will be something like the following:

var ball = new Bal();
john.push(ball);
addChild(ball);

This will go inside your timer-triggered function, obviously.

Secondly, you want to have a hitTestObject with henk and all of the balls stored in the john array.

for(var i = 0; i < john.length; i++) {
    if (henk.hitTestObject(john[i])) {
        // well, that's a bummer for your player, henk hit one of the balls in the john array
        // display something like a message here
    }
}

This will automatically detect the size of the array, so all elements are tested. Be careful with hitTestObject when you have a lot of elements in the john-array, this can slow down your game drastically.

Furthermore, reflecting your code I suggest the following:

  • remove public var i:Number = 2000; and public var k:Number = 9000;, these have no meaning anymore
  • use a mouse event to move your henk object, not an ENTER_FRAME. I guess you will be able to find how this works. This will only trigger the function when it has to do something, resulting in less CPU-power needed and a cleaner code.
  • if you want to make the game even cooler, you could add the support for using the arrow keys
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top