Copy link to clipboard
Copied
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.
Here is:
MAIN GAME.zip - Google Drive
I hope this is what you want.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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()
{
}
}
}
Copy link to clipboard
Copied
So do I put the code about the coin into where I have the rest of my code, or does it go into a separate file of its own ?
Copy link to clipboard
Copied
It must go into a separate file and it must be called 'Coin.as' and to make things easier it must be in the same folder where your FLA is located.
Copy link to clipboard
Copied
Ok i've got the separate file. Should the code look like this?
package
{
import flash.display.MovieClip;
public class Coin extends MovieClip
{
public function Coin()
{
}
}
}
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, loo
p);
Or does it need to go inside the package bit?
Thank you!
Copy link to clipboard
Copied
Like this:
Coin.as code:
package
{
import flash.display.MovieClip;
public class Coin extends MovieClip
{
public function Coin()
{
}
}
}
FLA 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);
Please let me know if it works now.
Copy link to clipboard
Copied
Doesn't seem to be working for some reason 😕 I had to delete var score:uint = 0; as I already had an identical one, as well as var currentLevel:uint = 0 as I already have it too, but it's set to 1.
I also had to change the name of the function loop as I already have one named that.
When I go over the coins, nothing happens, so they don't disappear and nothing is added to the score 😕
Copy link to clipboard
Copied
Sorry. I should've stated that this code is a possible application, but it's not supposed to work as it is for your case.
Anyway, here is a working example:
animate_cc_as3_coins_2.zip - Google Drive
Please let me know if it works now.
Copy link to clipboard
Copied
I don't understand, it's exactly the same as in the example You've uploaded and still doesn't work 😕
I'm getting "Type was not found or was not a compile-time constant: Coin".
Would you be willing to take a look at it if I uploaded it somewhere?
Copy link to clipboard
Copied
Surely!
Please share your file and I'll take a look.
Copy link to clipboard
Copied
MAIN GAME.rar - Google Drive Here it is.
Thank you!
Copy link to clipboard
Copied
Here is:
MAIN GAME.zip - Google Drive
I hope this is what you want.
Copy link to clipboard
Copied
Exactly what I was after! Thank you so much!
Just one quick question, I've got that screen that appears with a bird flying across it when the application is launched. Is there any way of making it so that it automatically switches to the Main Menu frame after a certain amount of time has passed, i.e. 3 seconds? I did some research and apparently a combination of gotoAndStop + a Timer can do it.
Thanks once again!
Copy link to clipboard
Copied
That's great!
Sure there is. Just put this code in beginning of that frame:
import flash.utils.setTimeout;
setTimeout(nextFrame, 3000);
You can use the Timer class, but in this way is easier.
Copy link to clipboard
Copied
Thanks again for all the help and this, works like a charm
Copy link to clipboard
Copied
You're welcome!
Copy link to clipboard
Copied
Hey i've noticed a slight problem with it. The coin collection works on level 1, however, on levels 2 and 3, when I go over them, nothing happens 😕 Any idea how to fix this ?
Copy link to clipboard
Copied
Hi again!
When I edited your file, I didn't see the other coins. I saw that your level Movie Clip has three frames and that there are two coins in the first frame. Are there others?
Anyway, for being able to collect coins in other frames/symbols, you have to reassign the coins variable
coins = getCoins(back.other);
wherever is needed and also update the container in this function call and in the for loop responsible for checking the coins collisions.
Copy link to clipboard
Copied
Makes sense, I've managed to do it, thank you once again!
Copy link to clipboard
Copied
Excellent!
Please don't hesitate to ask if you have any further questions.
Regards,
JC
Copy link to clipboard
Copied
I think I do, a very last question, when I load my game up and it goes to the loading screen and then the main menu screen, there are two gray bars on either side of it, but when it goes onto the game, the games goes full screen. Is there any way to change this in the loading & main menu so it also fills the whole thing?
Also, I've noticed a problem with the coins again 😕 For some reason I cannot see them in levels 2 and 3, even though I have put them in, on the back.other thing. I've made these changes to the code for it;
function gotoLevel2(): void
{ //Creates a function called gotoLevel2
back.other.gotoAndStop(2); //Makes the back.other symbol go to the 2 frame
back.visuals.gotoAndStop(2); //Makes the back.visuals symbol go to the 2 frame
back.collisions.gotoAndStop(2); //Makes the back.collisions go to the 2 frame
scrollX = 0; //Sets the scrollX variable to 0
scrollY = 500; //Sets the scrollY variable to 500
keyCollected = false; //Sets the keyCollected variable to false
back.other.doorKey.visible = true; //Makes the door key visible
doorOpen = false; //Makes the door locked
back.other.lockedDoor.gotoAndStop(1); //Makes the door graphic go to the first frame
coins = getCoins(back.other);
And the same for the level 3 function.
Copy link to clipboard
Copied
You have basically two options:
- [EASY] Add extra artworks outside the stage area (e.g.: repeating your mosaic background) to fill up the gray are when the game is running in screens with a aspect ratio that is not equal to 4:3, like your game.
- [HARD] Make your game responsive. Choosing this option, you code your SWF content to not be automatically resized and positioned, because you are going to do this by yourself. This is the ideal scenario and the most used nowadays because there are countless types of screens and devices that can be possible used to see your game/app.
Copy link to clipboard
Copied
Thank you, I've done it by extending the background
I'm adding and dropping them onto the stage with the symbol "Coin" from the library
Find more inspiration, events, and resources on the new Adobe Community
Explore Now