Be a Supporter!

Help to display Array of MovieClip

  • 353 Views
  • 8 Replies
New Topic Respond to this Topic
nst-99
nst-99
  • Member since: Nov. 26, 2012
  • Offline.
Forum Stats
Member
Level 01
Blank Slate
Help to display Array of MovieClip 2012-11-26 11:45:59 Reply

I 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;
}
}

}

}

nst-99
nst-99
  • Member since: Nov. 26, 2012
  • Offline.
Forum Stats
Member
Level 01
Blank Slate
Response to Help to display Array of MovieClip 2012-11-26 11:49:28 Reply

change function setupgame() with function storecoininarray

ProfessorFlash
ProfessorFlash
  • Member since: Oct. 6, 2007
  • Offline.
Forum Stats
Member
Level 32
Programmer
Response to Help to display Array of MovieClip 2012-11-26 12:15:25 Reply

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.

egg82
egg82
  • Member since: Jun. 24, 2006
  • Offline.
Forum Stats
Supporter
Level 05
Game Developer
Response to Help to display Array of MovieClip 2012-11-26 13:04:14 Reply

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

BBS Signature
milchreis
milchreis
  • Member since: Jan. 11, 2008
  • Offline.
Forum Stats
Member
Level 26
Programmer
Response to Help to display Array of MovieClip 2012-11-26 13:40:39 Reply

At 11/26/12 01:04 PM, egg82 wrote: never use star on imports. It's such a massive waste.

waste of what?

egg82
egg82
  • Member since: Jun. 24, 2006
  • Offline.
Forum Stats
Supporter
Level 05
Game Developer
Response to Help to display Array of MovieClip 2012-11-26 14:08:36 Reply

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

BBS Signature
nst-99
nst-99
  • Member since: Nov. 26, 2012
  • Offline.
Forum Stats
Member
Level 01
Blank Slate
Response to Help to display Array of MovieClip 2012-11-26 14:11:46 Reply

Thank you all for help

milchreis
milchreis
  • Member since: Jan. 11, 2008
  • Offline.
Forum Stats
Member
Level 26
Programmer
Response to Help to display Array of MovieClip 2012-11-26 14:52:36 Reply

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

egg82
egg82
  • Member since: Jun. 24, 2006
  • Offline.
Forum Stats
Supporter
Level 05
Game Developer
Response to Help to display Array of MovieClip 2012-11-26 15:21:30 Reply

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

BBS Signature