Skip to main content
Known Participant
March 5, 2018
Answered

Cannot collect more than 1 coin on the same level

  • March 5, 2018
  • 1 reply
  • 2789 views

Hey, it's me again

I've got a slight problem with my scoring system in my game. It's got 3 levels, and I wanted to have about 3 coins to collect on each level, however, I've noticed that only one of those coins can be collected, even though they are identical (dragged and drop the "coin" symbol several times and gave each coin an instance name of "coin") .

My code for the coin:

function enterFrameHandler(e:Event):void //Creates a function called enterFrameHandler, which is based on an Event

{    

    if(!back.other.coin.collision) //If collision occurs on the coin

    { 

        if (player.hitTestObject(back.other.coin)) //If player hits the coin

        {  

            score++; //+1 added to Score

            coins_txt.text = score.toString(); //Displays the score

            back.other.coin.visible = false; //Makes the coin invisible

            back.other.coin.collision = true; //Makes the collision of the coin true

        } 

    } 

}

Anyone got any idea what's wrong with it?

Thanks.

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

MAIN GAME.rar - Google Drive Here it is.

Thank you!


Here is:
MAIN GAME.zip - Google Drive

I hope this is what you want.

1 reply

JoãoCésar17023019
Community Expert
Community Expert
March 5, 2018

Hi again!

It's because you are using the coin's instance name to check for the collision. It will always return a reference to one single instance. You should refer to the coin's class name if you want it to get any coin.

Here is a suggestion:

Export your coin symbol for ActionScript:

Code:

import flash.events.Event;

import flash.display.DisplayObjectContainer;

import flash.display.MovieClip;

import flash.geom.Point;

var score:uint = 0;

var coins:Vector.<Coin> = new Vector.<Coin>();

var level:MovieClip;

var goals:Object =

{

    level0:3,

    level1:3,

    level2:3

};

var currentLevel:uint = 0;

var respawnX:Number;

var respawnY:Number;

function loop(e:Event):void

{

    if (score == goals["level" + currentLevel])

    {

        gotoNextLevel();

        return;

    }

      

    for (var i:int = coins.length - 1; i >= 0; i--)

    {

        if (player.hitTestObject(coins))

        {

            score++;

            coins_txt.text = score.toString();

            level.removeChild(coins);

            coins.splice(i, 1);

        }

    }  

}

function getCoins(container:DisplayObjectContainer):Vector.<Coin>

{

    var vec:Vector.<Coin> = new Vector.<Coin>();

    for (var i:int = 0, total = container.numChildren; i < total; i++)

        if (container.getChildAt(i) is Coin)

            vec = container.getChildAt(i) as Coin;

    return vec;

}

function gotoNextLevel():void

{

    nextFrame();

  

    if (currentFrame == totalFrames)

        return;

  

    currentLevel++;  

    score = 0;

    coins_txt.text = "0";

    level = this['back' + currentLevel].other;

    coins = getCoins(level);

    player.x = respawnX;

    player.y = respawnY;

}

stop();

level = this['back' + currentLevel].other;

coins = getCoins(level);

respawnX = player.x;

respawnY = player.y;

stage.addEventListener(Event.ENTER_FRAME, loop);

I hope it helps.

Regards,

JC

xKetjowAuthor
Known Participant
March 5, 2018

Hey, thanks for the reply!

It's giving me an error of "An ActionScript file must have at least one externally visible definition".

When I exported the coin for ActionScript, it said it'd create an ActionScript file automatically, but I wasn't able to find it so I just created one myself and named it coin with the .as extension. Would this be the cause of it?

JoãoCésar17023019
Community Expert
Community Expert
March 5, 2018

This message is thrown when we want to export a symbol for ActionScript but we didn't actually create a AS file. So one 'will be automatically generated  in the SWF file upon export'.

You can simply click 'OK'.

But if you really wish to create your own, it should look like this:

package

{

    import flash.display.MovieClip;

  

    public class Coin extends MovieClip

    {

        public function Coin()

        {

        }

    }

}