0
votes

I am working on a Uni project but I can't seem to figure one thing out.

On my stage I've got one object (peter_pan) and six children of other object (bottle_mc).

What I want to do is when one of those children hits Peter pan, it should disappear immediately and there should be +1 to score (yes, it's a really simple flash game).

However I can't seem to manage that because they disappear once they flow through him (and it's also a random bottle_mc not the one he touches) and the score always stops on 23 instead of one.

I am new at flash so my code is simple (also, sorry for my English I am not a native English speaker).

Here is my code:

stop();

var collisionArray:Array = new Array();

var damageArray:Array = new Array();

var peter_pan:Peter=new Peter;
addChild(peter_pan);
peter_pan.x=0;
peter_pan.y=200;
stage.addEventListener(KeyboardEvent.KEY_DOWN, movement);

function movement(event:KeyboardEvent):void {
    switch (event.keyCode) {
        case Keyboard.UP :
            peter_pan.y -=15;
            break;
        case Keyboard.DOWN :
            peter_pan.y +=15;
            break;
    }
}

stage.addEventListener(KeyboardEvent.KEY_DOWN, showUp);
function showUp(event:KeyboardEvent):void {
    if (peter_pan.y>=365) {
    peter_pan.y=8;
}
}

stage.addEventListener(KeyboardEvent.KEY_DOWN, showDown);
function showDown(event:KeyboardEvent):void {
    if (peter_pan.y<=5) {
    peter_pan.y=340;
}
}

for (var i:int = 0; i<6; i++) {
    var bottle_mc:Bottle=new Bottle;
    addChild(bottle_mc);
    bottle_mc.x = 540;
    bottle_mc.y = Math.floor(Math.random()*(350 -0 +1))+0;
    bottle_mc.hit = false;
    bottle_mc.addEventListener(Event.ENTER_FRAME, movingBottles);
    collisionArray.push(bottle_mc);
}

function movingBottles(e:Event):void {
    e.currentTarget.x-=5;
    if (e.currentTarget.x<10) {
        e.currentTarget.x = 540;
        e.currentTarget.y = Math.floor(Math.random()*(300 - 0 +1))-0;
        if (e.currentTarget.visible==false) {
            e.currentTarget.visible=true;
            e.currentTarget.hit = false;
        }
    }
}

var score_val:Number=0;
score_txt.text=String(score_val);

addEventListener(Event.ENTER_FRAME, damage);

function damage(e:Event):void {
    if (peter_pan.x>=bottle_mc.x-peter_pan.width) {
        bottle_mc.visible=false;
        score_val++;
        score_txt.text=String(score_val);
    }
}

So, how can I do simple thing like when peter_pan touches first bottle_mc it disappears straight away so he could collect them more and they would disappear and score would be only 1 point for 1 bottle?

1

1 Answers

1
votes

There seem to be a few issues in your code, but I think the main problem is in the damage function. You are comparing the x value of peter_pan and bottle_mc (which I think is meant to be the container of all six of the bottles).

Instead, you should be doing a hit test between peter_pan and each of the bottles individually. All of the bottles are already contained in the collisionArray array, so the code could look something like this:

function damage(e:Event):void {
    for (var i:int = 0; i < collisionArray.length; i++ ) {
        if (peter_pan.hitTestObject(collisionArray[i])) {
            collisionArray[i].visible = false;
            score_val++;
            score_txt.text=String(score_val);
        }
    }
}