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

copy a movieclip

New Here ,
Apr 15, 2014 Apr 15, 2014

Copy link to clipboard

Copied

hi,

I wanna have the same function of movieclip running next to the same one.How can I do that?

this is my code:

import flash.display.Graphics; import flash.display.MovieClip; import flash.events.Event; import flash.events.TimerEvent; import flash.utils.Timer;     var objectSpawner:Timer; var fallers:Array; initGame();     function initGame():void {           fallers = [];           objectSpawner = new Timer(1000);           objectSpawner.addEventListener(TimerEvent.TIMER, createEnemy);           objectSpawner.start();           addEventListener(Event.ENTER_FRAME, dropEnemies); }     function createEnemy(e:TimerEvent):void {           var enemy:Faller = new Faller();           enemy.y = -stage.stageHeight;           enemy.x = Math.random() * 380;           MovieClip(enemy).cacheAsBitmap = true;           addChild(enemy);           fallers.push(enemy);           drawConnectors(); }     function dropEnemies(e:Event):void {           trace(fallers.length);           for each (var mc:Faller in fallers)           {                     mc.y += 10;                     if (mc.y > stage.stageHeight * 2)                               fallers.splice(fallers.indexOf(removeChild(mc)), 1);           }           drawConnectors(); }     function drawConnectors():void {           if (fallers.length == 0)                     return;           var g:Graphics = this.graphics;           g.clear();           g.lineStyle(1);           var mc:Faller = fallers[0];           g.moveTo(mc.x, mc.y);           for each (mc in fallers)                     g.lineTo(mc.x, mc.y);     } 

TOPICS
ActionScript

Views

719

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

correct answers 1 Correct answer

LEGEND , Apr 15, 2014 Apr 15, 2014

Generally speaking, there is no a streamlined way to literally duplicate instances. So, to create a "duplicate" you should create another instance of the same class.

The code below does that - it creates two instance of the same class that (the class) implements your functionality

Logic that accomplishes the functionality of falling object is now encapsulated into a special class FallingThings. Place this class into the same directory where you FLA. To create class (if you don't know yet how it is

...

Votes

Translate

Translate
Community Expert ,
Apr 15, 2014 Apr 15, 2014

Copy link to clipboard

Copied

you want what?

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
New Here ,
Apr 15, 2014 Apr 15, 2014

Copy link to clipboard

Copied

This movieclip is running on the left side of my screen and I want the same one to run on the right

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 ,
Apr 15, 2014 Apr 15, 2014

Copy link to clipboard

Copied

if you mean Faller class instances are appearing on the left side of your screen and you want that to change, adjust this line of code:

enemy.x = Math.random() * 380;

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
New Here ,
Apr 15, 2014 Apr 15, 2014

Copy link to clipboard

Copied

no , what I want is to have 2 Faller on the screen. 1 on the left side and 1 on the right.

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 ,
Apr 15, 2014 Apr 15, 2014

Copy link to clipboard

Copied

then use:

function createEnemy(e: TimerEvent): void {

    var enemy: Faller = new Faller();

    enemy.y = -stage.stageHeight;  //<-this looks screwy

    enemy.x = Math.random() * stage.stageWidth/2;

    MovieClip(enemy).cacheAsBitmap = true;

    addChild(enemy);

    fallers.push(enemy);

    var enemy1: Faller = new Faller();

    enemy1.y = -stage.stageHeight;  //<-this looks screwy

    enemy1.x = stage.stageWidth/2+Math.random() * stage.stageWidth/2;

  addChild(enemy1);

    fallers.push(enemy1);

    drawConnectors();

}

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
LEGEND ,
Apr 15, 2014 Apr 15, 2014

Copy link to clipboard

Copied

Generally speaking, there is no a streamlined way to literally duplicate instances. So, to create a "duplicate" you should create another instance of the same class.

The code below does that - it creates two instance of the same class that (the class) implements your functionality

Logic that accomplishes the functionality of falling object is now encapsulated into a special class FallingThings. Place this class into the same directory where you FLA. To create class (if you don't know yet how it is done):

1. Create a files in the same directory as you FLA.

2. Name this file FallingThings.as.

3. Paste the code below into this file.

On the main timeline place this code:

init();

function init():void

{

          var fallingThingsLeft:FallingThings = new FallingThings(stage.stageWidth / 2, stage.stageHeight);

          var fallingThingsRight:FallingThings = new FallingThings(stage.stageWidth / 2, stage.stageHeight);

          addChild(fallingThingsLeft);

          addChild(fallingThingsRight);

          fallingThingsRight.x = stage.stageWidth / 2;

}

Class:

package

{

          import flash.display.Graphics;

          import flash.display.MovieClip;

          import flash.display.Sprite;

          import flash.events.Event;

          import flash.events.TimerEvent;

          import flash.utils.Timer;

          public class FallingThings extends Sprite

          {

                    // set width

                    private var w:Number = 0;

                    // set height

                    private var h:Number = 0;

                    private var objectSpawner:Timer;

                    private var fallers:Array;

                    private var speed:Number = 10;

                    public function FallingThings(w:Number, h:Number)

                    {

                              this.w = w;

                              this.h = h;

                              init();

                    }

 

                    private function init():void

                    {

                              fallers = [];

                              objectSpawner = new Timer(1000);

                              objectSpawner.addEventListener(TimerEvent.TIMER, createEnemy);

                              objectSpawner.start();

                              addEventListener(Event.ENTER_FRAME, dropEnemies);

                    }

 

                    private function createEnemy(e:TimerEvent):void

                    {

                              var enemy:Faller = new Faller();

                              enemy.y = -h;

                              enemy.x = Math.random() * w;

                              MovieClip(enemy).cacheAsBitmap = true;

                              addChild(enemy);

                              fallers.push(enemy);

                              drawConnectors();

                    }

 

                    private function dropEnemies(e:Event):void

                    {

                              for each (var mc:Faller in fallers)

                              {

                                        mc.y += speed;

                                        if (mc.y > h * 2)

                                                  fallers.splice(fallers.indexOf(removeChild(mc)), 1);

                              }

                              drawConnectors();

                    }

 

                    private function drawConnectors():void

                    {

                              if (fallers.length == 0)

                                        return;

                              var g:Graphics = this.graphics;

                              g.clear();

                              g.lineStyle(1);

                              var mc:Faller = fallers[0];

                              g.moveTo(mc.x, mc.y);

                              for each (mc in fallers)

                                        g.lineTo(mc.x, mc.y);

                    }

 

          }

}

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
New Here ,
Apr 16, 2014 Apr 16, 2014

Copy link to clipboard

Copied

You are a BOSS Andrei!
thank you so much

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
New Here ,
Apr 17, 2014 Apr 17, 2014

Copy link to clipboard

Copied

LATEST

I`ve got a last question when I hiTestObject with this code:

import flash.events.Event;

this.addEventListener( Event.ENTER_FRAME, handleCollision)

function handleCollision( e:Event ):void {

if (fallingThingsLeft.hitTestObject(Touch1)) {

score_txt.text = "HIT" }

else {

score_txt.text = "MISS" } }

It looks like it works but it tells me "HIT" like 3 sec before it really hit

And thanks again for all your help

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