开发者

Sprites and Arrays as3

开发者 https://www.devze.com 2023-02-21 01:50 出处:网络
I have a sprite called food which adds to the screen via the use of a timer. more food items are added to the screen over time. How would i hit test all these food sprites with another object?

I have a sprite called food which adds to the screen via the use of a timer. more food items are added to the screen over time. How would i hit test all these food sprites with another object? (the makeItem function creates the sprite btw)

 private function placeFood(event:TimerEvent = null):void{   
        var rndI:uint = Math.floor(Mat开发者_开发知识库h.random() * DIM);  //sets a random integer based on the the floor
        var rndJ:uint = Math.floor(Math.random() * DIM);

        var rndX:Number = grid[rndI][rndJ].x; // sets a grid position for the food item to go
        var rndY:Number = grid[rndI][rndJ].y;



        food = makeItem(Math.random() * 0xFFFFFF);// random color
        food.x = rndX;
        food.y = rndY;

        var foodArray:Array = new Array();
        foodArray.push(food);
        trace(foodArray.length)

        addChild(food); //adds the food to the board


        for (var i:uint = 0; i < snake.length; i++){
            if (rndY == snake[i].y && rndX == snake[i].x){ 
                placeFood();
            }
        } 
    }


Add the food items to an array and loop through that array doing hitTestObject. Something like:

var foods:Array = new Array();
foods.push(makeItem(blah));
...
for each (food in foods) {
  food.hitTestObject(object);
}


It looks like you're putting items on a fixed grid. Do the food items move around? Your food does not move, and your snake (or whatever collides with the food) does, you can greatly optimize your collision detection by figuring out what grid square(s) the moving object occupies and only checking against food in that local area.

Generally speaking, when you need to do collision detection between many objects, you do it in multiple passes. The first pass would consist of the least computationally expensive checks to cull things that could not possibly be colliding such as objects that are very far apart, moving away from each other, etc. Your next pass might be something like simple boundary box or circle tests. Lastly, when you're down to the few items that pass all the cheap tests, you can use more expensive and accurate hit tests like pixel-by-pixel ones between hit masks.


Another way avoiding arrays is to use a sprite that contains all food. Every sprite is a collection of sprites and therefore a tree. That's what I use for hit detection: 1 special sprite contains only enemies / food. Run through all children and you don't even need to check the types of them. Cast them if needed.

// begin
var foodCollection: Sprite = new Sprite();

// time passes, food is added
foodCollection.addChild(food);

// hit test
for (var i:int = 0; i < foodCollection.NumChildren; i++)
{
    var food: Sprite = foodCollection.getChildAt(i);
    // test
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号