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

Library Item to Stage

Community Beginner ,
Sep 17, 2013 Sep 17, 2013

I have a library item that is called tomato and AS linkage called tomatoPieces.  I am trying to get it to add the item to the stage and then randomally move the item around.  Here is the code so far but the problem it seems to stop add the addEventListener to the stage.  I would appreciate any help or direction. Thanks..

import flash.display.Sprite;

import flash.display.MovieClip;

import flash.events.*;

var newX:Number = 0;

var newY:Number = 0;

var speed:Number;

var speedX:Number;

var speedY:Number;

var totalDistance:Number;

var previousDistance:Number = 0;

var currentDistance:Number = 0;

function main()

{

          for (var i:Number = 0; i<9; i++)

          {

                    trace(i);

                    var pieces:tomatoPiece = new tomatoPiece();

                    pieces.scaleX = .25;

                    pieces.scaleY = .25;

                    addChild(pieces);

                    //trace(this.addChild(pieces).name);

                    tomatoPieces();

          }

}

function tomatoPieces()

{

          trace("setup listener");

          addEventListener(Event.ADDED_TO_STAGE, Setstage);

}

function Setstage(event:Event):void

{

          trace("Setup");

          this.x = this.GetRandomXPosition();

          this.y = this.GetRandomYPosition();

          speed = Math.round(.5 + Math.random() * 5);

          this.addEventListener(Event.ENTER_FRAME, MoveCircle);

}

function GetRandomXPosition():Number

{

          //

          //basic formula: Math.floor(Math.random()*(1+High-Low))+Low;

          //

          return Math.floor(Math.random() * (1+ (stage.stageWidth + this.width) + this.width) - this.width);

}

function GetRandomYPosition():Number

{

          //

          //basic formula: Math.floor(Math.random()*(1+High-Low))+Low;

          //

          return Math.floor(Math.random() * (1+ (stage.stageHeight + this.height) + this.height) - this.height);

}

function MoveCircle(e:Event)

{

          this.previousDistance = this.currentDistance;

          this.currentDistance = this.GetDistance();

          if (this.currentDistance < this.previousDistance)

          {

                    this.x +=  this.speedX;

                    this.y +=  this.speedY;

          }

          else

          {

                    this.SetNewPosition();

          }

}

function GetDistance():Number

{

          return Math.sqrt(Math.pow(this.x - this.newX,2) + Math.pow(this.y - this.newY,2));

}

function SetNewPosition()

{

          this.newX = this.GetRandomXPosition();

          this.newY = this.GetRandomYPosition();

          this.totalDistance = this.GetDistance();

          var time:Number = this.totalDistance / this.speed;

          speedX = (this.newX - this.x)/time;

          speedY = (this.newY - this.y)/time;

}

TOPICS
ActionScript
1.3K
Translate
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 , Sep 18, 2013 Sep 18, 2013

That's what I expected.  If you want to move the pieces you need to target the pieces, not the main timeline.  Everywhere you use "this" you are targeting the main timeline, not the pieces.

If you were to move all of that placement/movement code inside of each piece then it would make more sense to be using "this" for that code.  That would also make more sense for the ADDED_TO_STAGE listener you were implementing earlier. That is one way to solve this.  After you create each piece you let each p

...
Translate
LEGEND ,
Sep 17, 2013 Sep 17, 2013

I doubt anything is going to be triggering your ADDED)TO STAGE listener.  Try replacing the line

                    tomatoePieces(); // get rid of the function altogether

with

                    setstage();

and change your Setstage function to remove the event (I changed setstage to lowercase - uppercase is normally used to indicate a class name)....

                   function setstage():void

and after doing those things see what else you want to address as far as moving this around... your movement function seems limited in its scope

Translate
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 Beginner ,
Sep 18, 2013 Sep 18, 2013

Ned

Thanks that seems to fix it except all 9 of the moviesclips move ontop of each other... I want them all to move seperate and random. How do I fix that?

Translate
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 ,
Sep 18, 2013 Sep 18, 2013

Focus on what your code is doing to place and move the objects.

In your setstage function you don't appear to be placing/moving any of them, instead you are targeting "this"

          this.x = this.GetRandomXPosition();

          this.y = this.GetRandomYPosition();

Try tracing "this" just to see what you are actually targeting.  Then you might see what you are actually moving around as well.

Translate
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 Beginner ,
Sep 18, 2013 Sep 18, 2013

when I trace it I get [object MainTimeline]

Translate
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 ,
Sep 18, 2013 Sep 18, 2013

That's what I expected.  If you want to move the pieces you need to target the pieces, not the main timeline.  Everywhere you use "this" you are targeting the main timeline, not the pieces.

If you were to move all of that placement/movement code inside of each piece then it would make more sense to be using "this" for that code.  That would also make more sense for the ADDED_TO_STAGE listener you were implementing earlier. That is one way to solve this.  After you create each piece you let each piece manage its own movement by having that code within each.

Another way to manage it would be to store references to the pieces in an array and have the movement function loop thru the array to move each piece one after another.

Translate
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 Beginner ,
Sep 20, 2013 Sep 20, 2013

Hey Thanks.  hHat worked putting inside the piece.

Translate
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 ,
Sep 20, 2013 Sep 20, 2013
LATEST

You're welcome

Translate
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