Question

I am making a horizontal side scroll game in which the objective of the player is to get as far as he possibly can in the game. In order to play the game, the player will have to jump over objects placed on the ground (rocks). He will also have to destroy objects (ice) that are hanging from the top of the stage by shooting an object (a ball for now) at these ice objects. I have managed to make the jumping part of the game work, I am having difficulties however in trying to make the player destroy the ice objects.

The ice objects are Movieclips being placed in a container on the stage using a for loop and some math randoms. The player can shoot an object at these ice objects to destroy them. I created a function that is activated when the ball of the player hits the ice objects. Both of the objects will dissappear upon hit and should be removed from the container. This function does appear to remove both the ice object that is hit and the ball that the player shot from the stage. However both objects are still in the container, you just can't see them. Making it still possible for the player to hit the ice object and die. The ball that the player shot (which is invisible) also keeps on moving and destroying all the ice objects that are being spawned in the container, destroying all the ice objects before the player has even seen them. I have tried several methods to fix this problem, but whenever I fix the problem another one seems to be created.

here's my code for spawning the ice objects and making them move, scrolling from right to left:

/*placing ice objects in container cave*/

var k:int = 0;
var p:MovieClip;
for (k=0; k<=50; k++)
{
    var scalexwaarde = Math.random();
    if (scalexwaarde<0.4)
    {
        scalexwaarde = 0.4;
    }
    else if (scalexwaarde>0.8)
    {
        scalexwaarde = 0.8;
    }

    var scaleywaarde = Math.random();
    if (scaleywaarde<0.3)
    {
        scaleywaarde = 0.3;
    }
    else if (scaleywaarde>0.9)
    {
        scaleywaarde = 0.9;
    }
    var rndm:int = (Math.floor(Math.random()*100))+1;
    p = new ice();
    cave.addChild(p);
    p.y = 230;
    p.scaleX = scalexwaarde * 1.1;
    p.scaleY = scaleywaarde * 1.1;
    p.x = 800 + (220 * rndm);
    p.addEventListener(Event.ENTER_FRAME, move_ice);
    p.addEventListener(Event.ENTER_FRAME, raakSnot);
    p.addEventListener(Event.ENTER_FRAME, raken);
}

/*function for moving ice objects*/

var snelh:int = 9;
function move_ice(e:Event):void
{
    e.target.x -=  snelh;
}

code for creating object that player can shoot and making it move:

/*creating the object*/
var s:MovieClip = new sneeze(); 

/*if the space is clicked, add to container cave*/
if (evt.keyCode == Keyboard.SPACE) // sneeze when pressed
    {
            cave.addChild(s);
            s.x = n.x;
            s.y = n.y;
            s.addEventListener(Event.ENTER_FRAME, niezen);
    }

/*making player object move and define speed*/
var snelhsneeze:int = 15;
function niezen(evt:Event):void
{
    evt.target.x +=  snelhsneeze;
}

code for handling hits of any object:

/*function that switches to game over screen when
the player hits an object.*/

function raken(evt:Event):void
{
    if (evt.target.hitTestPoint(n.x - 15,n.y,false) == true)
    {
        gotoAndStop(5);
        grondGeraakt = false;
        scrollSpeed = 0;
        speed = 0;
        snelh = 0;

        while (cave.numChildren)
        {
            cave.removeChildAt(0);
        }
    }
    else if (grondGeraakt == false)
    {

    }
}

code for handling the hittest of ice objects (PROBLEM HERE):

/*FUNCTION THAT IS GIVING PROBLEMS*/
/*function activated when ball object hits ice object*/

function raakSnot(evt:Event)
{
    if (evt.target.hitTestPoint(s.x,s.y-15,false) == true) 
    {
        if(s.parent) // remove child s and its events (ball object)
        {
            s.parent.removeEventListener(Event.ENTER_FRAME, niezen);
            s.parent.removeChild(s);
        }
        if(MovieClip(evt.target).parent) // remove ice object and its events
        {
            MovieClip(evt.target).parent.removeEventListener(Event.ENTER_FRAME, raken);
            MovieClip(evt.target).parent.removeChild(MovieClip(evt.target));
        }
    }
}

Any help on the problem would be highly appreciated as i really don't know what is wrong. Thanks in advance!

Was it helpful?

Solution

Even though you have removed the object, all active ice is still hitTesting against the point specified (not the object, which is being removed but retains it's x and y property values). What you should do is only check for the collision if a boolean like "sneezeActive" is true.

if (evt.target.hitTestPoint(n.x - 15,n.y,false)&&sneezeActive)

If a collision is detected, sneezeActive should be set to false. Every time the player sneezes, set sneezeActive to true. This way, the only time a collision can be detected based on that point is if the sneeze is active, after which it is deactivated until another sneeze is performed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top