1

I haven't coded since Macromedia, and god things have changed. I'm at a steep learning curve and trying to get back into it.

Im making a little flash game where the player collects tacos and the score goes up.

No code online that I have found for making collectable money works on Animate CC for me. I am unsure why that is. The script runs fine, debug detects nothing. But the code doesn't do anything.

I have tried this:

if(player.hitTestObject(taco1))
{
    if(back.contains(taco1))
        removeChild(taco1);

    score += 10;
    points.text = String(score);
}

which either doesnt collect the taco, or the taco follows the player (if I put it on the main scene.}

I've also tried just plugging in my instances in a code provided on AS3 game tutorials. It can be seen here, unaltered.

    if(keyCollected == false){ // if we still haven't collected the key
if(player.hitTestObject(back.doorKey)){ // and if the player collides 
with the key
back.doorKey.visible = false; // hide the key from view
keyCollected = true; // set our Boolean to true
}
}

none of this works either.

Sorry new to forum, and haven't coded in ten years. But any points in the right direction would be so helpful! Thank you!

1 Answers1

1

It seems that you should wrap the hitTestObject inside a function and run it on an ENTER_FRAME event or on a TimerEvent. If no ENTER_FRAME or timer is present your code will run only once, therefore you see no effect.

From your code I can tell that you have a reference to the taco1 object and that object belongs to the back object.

if(back.contains(taco1))
    removeChild(taco1);

In the code above there is a problem. You should have:

if(back.contains(taco1))
    back.removeChild(taco1);

Other than that, the code block should work fine. For example using the ENTER_FRAME event your code will look as the code bellow:

this.addEventListener(Event.ENTER_FRAME, collectTaco);

function collectTaco(evt:Event):void {
    if(taco1 != null && player.hitTestObject(taco1)) {
        if(back.contains(taco1)) {
            back.removeChild(taco1);
            taco1 = null;
        }
        score += 10;
        points.text = String(score);
    }
} 

Note that there is an assumption that the taco1 object always holds a reference to a taco that you want to hit test with. That's why I nullify the taco1 after removing it from the back. After the taco was collected the if statement will evaluate to false, so won't try to collect it again. I think you'll create more tacos while the game goes on, and you must set a reference of the new taco to taco1.

If you have more tacos on the stage you should traverse all tacos (either store them in a Vector or just go through all children of the back parent, and maybe test them for being a Taco) and do the hit test for all of them.

Constantin
  • 601
  • 5
  • 14
  • This seems to run fine, but it alternates between doing nothing, or telling me "collecttaco" doesn't exist. Does it need a var? – theFoxtrotblues .Foxtrotblues Oct 13 '17 at 23:33
  • Then you might have the code snippet only on some frames. It should have a global scope on the timeline (as it seems your game is structured on the timeline). Can you confirm that? If not, can you describe the architecture? – Constantin Oct 14 '17 at 00:29