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

Newbie needs some help. i need to show a MC on the stage in 4 random positions?

New Here ,
Apr 15, 2013 Apr 15, 2013

i am new to actionscript and need some help with showing my MC in 4 places on the stage (topleft, topright, bottomleft, bottomright) all one after the other with a 2 second delay between them.

if any one has any actionscript 3.0 on how to do this youd be a life saver

TOPICS
ActionScript
742
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

Community Expert , Apr 15, 2013 Apr 15, 2013

the topleft etc are not random positions but, if MC has reg point at the top left:

addChild(MC);

MC.x=MC.y=0;

var positionA:Array=[[stage.stageWidth-MC.width,0],[0,stage.stageHeight-MC.height],[stage.stageWidth-MC-width,stage.stageHeight-MC.height]];

var t:Timer=new Timer(2000,3);

t.addEventListener(TimerEvent.TIMER,f);

t.start();

function f(e:TimerEvent):void{

MC.x=positionA[t.currentCount][0];

MC.y=positionA[t.currentCount][1];

}

Translate
Community Expert ,
Apr 15, 2013 Apr 15, 2013

the topleft etc are not random positions but, if MC has reg point at the top left:

addChild(MC);

MC.x=MC.y=0;

var positionA:Array=[[stage.stageWidth-MC.width,0],[0,stage.stageHeight-MC.height],[stage.stageWidth-MC-width,stage.stageHeight-MC.height]];

var t:Timer=new Timer(2000,3);

t.addEventListener(TimerEvent.TIMER,f);

t.start();

function f(e:TimerEvent):void{

MC.x=positionA[t.currentCount][0];

MC.y=positionA[t.currentCount][1];

}

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

okay so i understand the timer and that the function calls the array?

but i dont really understand the array? could you explain it? or what i need to change ion the array to customise it to my project?

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 Expert ,
Apr 15, 2013 Apr 15, 2013

nothing needs to be changed if MC reg point is at its top-left.

the array positions MC at the topleft, etc of your stage.  copy and paste the code and try it.

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

had to edit teh code just a bit because of some small punctuation errors, but this is great stuff!

currently goes top left bottom left and bottom right with a 2 second delay which is fantastic!

how do i edit the position of the MC? maybe add more positions where it shows on the stage?

sorry for the questions i have to show my understanding in my school project

addChild(MC);

MC.x=MC.y=0;

var positionA:Array=[[stage.stageWidth-MC.width,0],[0,stage.stageHeight-MC.height],[stage.stageWidth-MC.width,stage.stageHeight-MC.height]];

var t:Timer=new Timer(2000,3);

t.addEventListener(TimerEvent.TIMER,f);

t.start();

function f(e:TimerEvent):void{

MC.x=positionA[t.currentCount][0];

MC.y=positionA[t.currentCount][1];

}

f;

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 Expert ,
Apr 15, 2013 Apr 15, 2013

to show more positions, you would edit positionA to add more x,y positions.  each element of positionA is an array that contains an x,y position.

and, you edit the timer t so it makes more calls to f.

but, if you want to display your movieclip in random positions anywhere on stage, use:

var t:Timer=new Timer(2000,0);

t.addEventListener(TimerEvent.TIMER,f);

t.start();

function f(e:TimerEvent):void{

MC.x=Math.floor(Math.random()*(stage.stageWidth-MC.width))

MC.y=Math.floor(Math.random()*(stage.stageHeight-MC.height))

}

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

this is great! thanks very much

sooo say if i needed the MC to randomly go into just the 4 corners of the stage instead of all over, would i just put some coordinates in the bracets of the math.random

like this?

Math.random(x=?,y=?)

or would they be needed from the array? like this?

function f(e:TimerEvent):void{

MC.x=Math.floor(Math.random()*(PositionA))

MC.y=Math.floor(Math.random()*(PositionA))

}

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 Expert ,
Apr 15, 2013 Apr 15, 2013
LATEST

no, if you want to specify several locations, add the locations to an array like positionA.

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