Search code examples
actionscript-3colorsdetectionbounce

Is it possible to detect a collision on only a specific colour? AS3


I am working on a game and a border is drawn around a ball.

The border dimensions are taken from an XML file.

The edge of the border are made using:

    lineThick = Number(xmlContent.rec[i].look.strokethick);

    lineColor = int(xmlContent.rec[i].look.strokehex);

    drawings.graphics.lineStyle(lineThick, lineColor);

    drawings.graphics.beginFill(fillColor);

In a create drawings functions

Now what I want to know is how can I detect when my ball (that is moving around the screen) has hit the edge?

I thought about trying to make it detect when it hits the colour black because that is the colour of all the lines.

Is there a better way? is it possible to detect the colour black and make the ball bounce back?


Solution

  • This a part of the code i use for gaming, you might have to change it slightly to your own need: function testCollisions():void { for (var i:int = enemies.length -1;i >= 0;i--) { tempEnemy = enemies[i]; if (tempEnemy.hitTestObject(player)) { //this is the part to put the code for bouncing the ball back } } }

    Here i am testing collision of player on tempEnemy. i have all of my tempEnemy objects in the enemies array. this might not be the most efficient but it works.