• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

how to repeatedly duplicate mc with AS3

New Here ,
Aug 19, 2022 Aug 19, 2022

Copy link to clipboard

Copied

Hi, I need advice on how to duplicate MC from the library repeatedly. This code creates a copy of the MC (walking character) on the stage and then another function moves it. This works fine. But as soon as I click again, it doesn't work anymore. The first character stops and the second one goes 2x faster. I know that the problem is in the "akceb" variable, if they were named differently after each click, it would be fine. But I don't know how to do it. Please, give me an advice. Thank you

 

karta1Class=getDefinitionByName(karta1Name) as Class;
karta1Sprite=new karta1Class();
akceb = hraKartyKontejner.addChild(karta1Sprite);
akceb.addEventListener(Event.ENTER_FRAME, vyvolejPostavu);


function vyvolejPostavu(e:Event):void
{

//character shift
akceb.x = akceb.x+rychlost;
}

TOPICS
ActionScript , How to

Views

92

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 19, 2022 Aug 19, 2022

Copy link to clipboard

Copied

 isn't easy to see what is wrong. Is there a way to can upload the files for us to rtry?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 21, 2022 Aug 21, 2022

Copy link to clipboard

Copied

Hi.

 

I recommend you to add only one enterFrame listener. Something like this:

import flash.display.DisplayObject;
import flash.events.Event;
import flash.events.MouseEvent;

var spawned:Vector.<YourLibSymbol> = new Vector.<YourLibSymbol>();

function spawn(e:MouseEvent):void
{
	var instance:YourLibSymbol = new YourLibSymbol();
	
	instance.vX = 10;
	addChild(instance);
	spawned.push(instance);
}

function enterFrameHandler(e:Event):void
{
	var i:int;
	
	for (i = spawned.length - 1; i > -1; i--)
		spawned[i].x += spawned[i].vX;
}

yourButton.addEventListener(MouseEvent.CLICK, spawn);
stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);

 

If you want to improve performance event further, consider using object pooling.

 

Regards,

JC

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 21, 2022 Aug 21, 2022

Copy link to clipboard

Copied

LATEST

your code is problematic, if it executes more than once:

 

karta1Sprite=new karta1Class();

//you have duplicate objects named karta1Sprite.  


akceb = hraKartyKontejner.addChild(karta1Sprite);

// akcep = karta1Sprite, so this accomplishes nothing more than: hraKartyKontejner.addChild(karta1Sprite);

// the last created karta1Sprite is the only one able to be referenced by karta1Sprite (and/or akcep).

 

akceb.addEventListener(Event.ENTER_FRAME, vyvolejPostavu);

/* compounds the problem by repeatedly adding the same listener to the last created karta1Sprite. ie, if your shown code executes twice, you expect exactly the problem you described.  if it executes 10 times, the last karta1Sprite will move 10 x rychlost and the others will not move */


function vyvolejPostavu(e:Event):void
{

//character shift
akceb.x = akceb.x+rychlost;

// again, akceb is the last created karta1Sprite.
}

 

 

you're not going to get much performance benefit by pooling or using an array or using one enterframe loop unless there are more than 10**5 characters.

 

but using those things will help you keep organized.  but so will using a class file for kart1Class and generally is easier to maintain and debug.

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines