1

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!

4

1 回答 1

0

即使您已经移除了对象,所有活动的冰仍然会针对指定的点进行 hitTesting(不是对象,它正在被移除但保留它的 x 和 y 属性值)。如果像“sneezeActive”这样的布尔值是真的,你应该做的只是检查碰撞。

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

如果检测到碰撞,则 sneezeActive 应设置为 false。每次玩家打喷嚏时,将 sneezeActive 设置为 true。这样,基于该点可以检测到碰撞的唯一时间是喷嚏是否处于活动状态,之后它会被停用,直到执行另一个喷嚏。

于 2013-06-12T16:04:17.457 回答