0
votes

I am stuck at a very basic problem which I am not quite able to figure out. I have 4 alienships and a crosshair which moves around with the mouse. I want to change the color of my crosshair to red when it is near one of the alienships. Both my alienships and crosshair are classes which extend movieclip. I use them in my main document class inside a main game loop which is triggered by the Enter_Frame event listener.

Here is my logic that checks for the same

private var objColor:ColorTransform = new ColorTransform();

private function overlayCursorMove() : void {

     var initialColor:ColorTransform = new ColorTransform();
     initialColor.color = 0xD3D3D3;

            for(var i = 0; i < alienShipArray.length; i++){
                var currentShip:AlienShip = alienShipArray[i];
                if(currentShip.getDistance(overlayCursor.x - currentShip.x, overlayCursor.y - currentShip.y) <= 30 ){
                    overlayCursor.transform.colorTransform = objColor;
                }
                else if(currentShip.getDistance(overlayCursor.x - currentShip.x, overlayCursor.y - currentShip.y) > 30 ){
                    overlayCursor.transform.colorTransform = initialColor;
                }
            }
        }

Below is the custom getDistance function inside the AlienShip class

public function getDistance(delta_x:Number, delta_y:Number):Number
        {
            return Math.sqrt((delta_x*delta_x)+(delta_y*delta_y));
        }

overlayCursor is my crosshair which moves around with my mouseX and mouseY positions. I have 4 alienships on my stage and all of them are stored inside the alienShipArray.

I am just calculating the distance between my crosshair and the currentship position to check if its less than a certain amount to change the crosshair color else if distance is greater change it back to previous color.

Surprisingly the logic only works for the very last ship in the array that is when the loop reaches i = 3 ( alienShipArray[3] ). For the rest of the ship the color of the crosshair stays the same!

1

1 Answers

1
votes

You need to break out of your loop when you encounter an intersection, otherwise your loop will continue to run and the other non-intersecting aliens will cause the else if part of your condition to test true, and your crosshair will be changed back to its initial color.

That would explain why your code only works with the last alien in the loop, as there are no further iterations in that instance to reset the color.

Something like the following should do it (untested):

private var objColor:ColorTransform = new ColorTransform();

private function overlayCursorMove() : void {

    var initialColor:ColorTransform = new ColorTransform();
    initialColor.color = 0xD3D3D3;

    for(var i:uint = 0; i < alienShipArray.length; i++) {

        var currentShip:AlienShip = alienShipArray[i];

        // Reset cursor back to initial color
        overlayCursor.transform.colorTransform = initialColor;

        if(currentShip.getDistance(overlayCursor.x - currentShip.x, overlayCursor.y - currentShip.y) <= 30 ) {

            // Intersection found, change the cursor color
            overlayCursor.transform.colorTransform = objColor;

            // And break out of the loop so further iterations don't reset it
            break;
        }
    }
    // No intersections found, cursor remains in its initial state
}