Skip to main content
Known Participant
February 12, 2018
Answered

Coin value doesn't increase and doesn't disappear upon pickup

  • February 12, 2018
  • 1 reply
  • 1319 views

Hi,

I want to add a scoring system to my game, so upon pickup of the coin, it disappears, +1 to the Score. I've got the coins inside my background symbol and then another symbol used for miscellaneous things on the bground and it doesn't work in there, as I get the error 2007 - Parameter hitTestObject must be non-null, but when I take it out of this symbol and onto the main game, it works, but as the player moves, so does the coin. How do I make it work inside the symbol?

This is the code that I've got for the coin. I've gotten this from another tutorial, and I tried to follow the layout of the code for the key below, however, the code for the key is working, and this isn't.

var score=0;

if (player.hitTestObject(back.other.coin)) {

score++;

coins_txt.text=score.toString();

back.other.coin.visible = false;

}

This here is showing something similiar that I've got, a key, which I'm able to collect and it disappears when I do. This is basically what I want with my coins, with the addition of adding +1 to the current score.

if(keyCollected == false){

if(player.hitTestObject(back.other.doorKey)){

back.other.doorKey.visible = false;

keyCollected = true;

trace("key collected");

}

}

So far, I've tried Googling the error but I couldn't find anything useful apart from the fact that adding a brace somewhere may fix it, but I don't really know where, and also looked at different ways of doing it. Originally, I had the code for the coin in a Class which I linked to my main game afterwards, but I had a similiar problem there. I've also made sure that the coin has an instance name of "coin".

Does anyone have any suggestions how to fix this?

Thank you

This topic has been closed for replies.
Correct answer JoãoCésar17023019

Great!

Is this code running inside of a Event.ENTER_FRAME handler function?

And, yeah, you should add a verification to check if the coin has already been taken because if not the code inside the hit test will continue to run.

Something like this:

import flash.events.Event;

var score:uint = 0;

stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);

function enterFrameHandler(e:Event):void

{  

    if(!back.other.coin.collision)

    {

        if (player.hitTestObject(back.other.coin))

        {

            score++;

            coins_txt.text = score.toString();

            back.other.coin.visible = false;

            back.other.coin.collision = true;

        }

    }

}

1 reply

JoãoCésar17023019
Community Expert
Community Expert
February 12, 2018

Hi.

Please double check if you really have an object called 'coin' inside of another object called 'other' inside of another object called 'back'.

And you should create a collected verification like in the door key example because if not your score will receive +1 as long as your player collides with the coin. This is because the invisiblitiy doesn't stop the hit test.

xKetjowAuthor
Known Participant
February 12, 2018

Hey,

Thanks for the reply.

I double checked it and it wasn't named as "coin", which did get rid of the parameter hitTestObject error, however, the actual function still doesn't work, as nothing happens when the player and coin collide :/

How exactly would I go along doing the second part? Do you mean something like adding "if (coinCollected) = false?

xKetjowAuthor
Known Participant
February 12, 2018

Great!

Is this code running inside of a Event.ENTER_FRAME handler function?

And, yeah, you should add a verification to check if the coin has already been taken because if not the code inside the hit test will continue to run.

Something like this:

import flash.events.Event;

var score:uint = 0;

stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);

function enterFrameHandler(e:Event):void

{  

    if(!back.other.coin.collision)

    {

        if (player.hitTestObject(back.other.coin))

        {

            score++;

            coins_txt.text = score.toString();

            back.other.coin.visible = false;

            back.other.coin.collision = true;

        }

    }

}


Thanks a lot! This worked

However I tested it on multiple levels as I've got 3, and the coins cannot be picked up on levels 2 and 3. Do you know why this may be?

Thanks again!

EDIT: I forgot to give the coins on different levels the "coin" instance name. After doing this it started working Thanks!