Monster Racer Rush
Select between 5 monster racers, upgrade your monster skill and win the competition!
4.18 / 5.00 3,534 ViewsBuild and Base
Build most powerful forces, unleash hordes of monster and control your soldiers!
3.80 / 5.00 4,200 ViewsI have MovieClip as coin. I want to display it several time in screen at different position by using Array.
The problem is only last item in Array was display in screen. Could you help me!
This my code
package {
import flash.display.*;
import flash.events.*;
import flash.text.*;
import flash.ui.Keyboard;
import flashx.textLayout.formats.Float;
public class Main extends MovieClip {
var coinarray:Array;
var Coin:coin;
public function Main() {
Coin=new coin;
coinarray=new Array();
storecoininarray();
displaycoin();
}
function setupgame()
{
for (var i=0; i< 10; i++)
{
coinarray.push(Coin);
}
}
function displaycoin()
{
var k:int=0;
for (var i=0; i< 10; i++)
{
coinarray[i].x=300+k;
coinarray[i].y=300;
addChild(MovieClip(coinarray[i]));
k+=10;
}
}
}
}
change function setupgame() with function storecoininarray
You have only created one Coin. Create a new coin in the loop to get 10 of them.
You can solve pretty much any problem you may have with AS3 by consulting the AS3 Language reference.
use code tags.
import flash.display.*;
never use star on imports. It's such a massive waste. I guarantee you will never use every DisplayObject available to you. If you do, you're either rally new, or your game's really big.
public class Main extends MovieClip {
why? Just extend Sprite. You will never, ever, ever, ever, ever need a timeline while programming.
public class Main extends Sprite {
Coin=new coin;
you have your casing mixed up, and you're missing parenthesis. Also, make use of whitespace.
coin = new Coin();
you will also need to capitalize the Coin class.
var coinarray:Array;
Just use a vector. And camelcasing. And public/private. And underscores.
private var _coinArray:Vector.<Coin> = new Vector.<Coin>;
use:
public function Main() {
for (var i:uint = 0; i < 10; i++) {
_coinArray[_coinArray.length] = new Coin();
}
}
coinarray.push(Coin);
not exactly. You'll have to make a new coin for every array element, otherwise you're just giving it a reference to a single coin. Also, it's very slightly faster to use direct methods for accessing elements in an array, rather than using functions like push and pop.
var k:int=0;
uints are very slightly faster than ints with player 10+
var k:uint = 0;
for (var i=0; i< 10; i++)
{
coinarray[i].x=300+k;
coinarray[i].y=300;
addChild(MovieClip(coinarray[i]));
k+=10;
}
no. Try this instead:
for (var i:uint = 0; i< 10; i++) {
coinarray[i].x = 300 + (i * 10); //parenthesis for clarification
coinarray[i].y = 300;
addChild(coinArray[i]);
}
Coin better not be extending MovieClip, unless it contains a hand-drawn frame-by-frame animation. Even then, I recommend a sprite sheet and Bitmaps.
Programming stuffs (tutorials and extras)
PM me (instead of MintPaw) if you're confuzzled.
thank Skaren for the sig :P
At 11/26/12 01:04 PM, egg82 wrote: never use star on imports. It's such a massive waste.
waste of what?
At 11/26/12 01:40 PM, milchreis wrote: waste of what?
unless the compiler only imports what is used, it would be a waste of memory. Granted, memory comes somewhat cheap, but why pointlessly waste what you have?
Programming stuffs (tutorials and extras)
PM me (instead of MintPaw) if you're confuzzled.
thank Skaren for the sig :P
At 11/26/12 02:08 PM, egg82 wrote:At 11/26/12 01:40 PM, milchreis wrote: waste of what?unless the compiler only imports what is used
it does
At 11/26/12 02:52 PM, milchreis wrote: it does
ahh, good to know!
Programming stuffs (tutorials and extras)
PM me (instead of MintPaw) if you're confuzzled.
thank Skaren for the sig :P