Skip to main content
Inspiring
December 13, 2011
Answered

Access a movieclip within a class from native stage

  • December 13, 2011
  • 1 reply
  • 508 views

Ok so I find classes all about confusing but take a look at this example. A movieclip is created that follows the cursor, but it is created as a class.

http://asgamer.com/2009/as3-characte...se-with-easing

what do i put on the normal timeline of the MouseControlled.fla file to target the character that moves around? Like if I wanted to start coding some objects it might hit that I have on the root native timeline how do I reference it?

what is the pathway to the instance name?

This topic has been closed for replies.
Correct answer Colin Holgate

You can adapt a class example for timeline by removing the package line, removing the class line, removing the "constructor" opening, and deleting any "private" or "public" words. Also delete any extra closed braces at the end of the script, and the end of where the constructor function was.

In this example's case there is one line that you have to add something, which is where the stage reference is stored. Or you could replace that variable with "stage". In my version below I just put stage into that variable.

So, make a new movieclip on your FLA stage, go into the movieclip, and put this script into frame 1's timeline, then do a test movie:

import flash.display.MovieClip;

import flash.events.*;

import flash.display.Stage;

var stageRef:Stage = stage;

var speed:Number = 10;

x = stageRef.stageWidth / 2;

y = stageRef.stageHeight / 2;

this.stageRef = stageRef;

addEventListener(Event.ENTER_FRAME, loop, false, 0, true);

function loop(e:Event):void {

var yDistance:Number = stageRef.mouseY - y;

var xDistance:Number = stageRef.mouseX - x;

if (Math.sqrt(yDistanceyDistance + xDistancexDistance) < speed) {

x = stageRef.mouseX;

y = stageRef.mouseY;

} else {

var radian:Number = Math.atan2(yDistance,xDistance);

x += Math.cos(radian) * speed;

y += Math.sin(radian) * speed;

rotation = radian * 180 / Math.PI;

}

1 reply

Colin Holgate
Colin HolgateCorrect answer
Inspiring
December 13, 2011

You can adapt a class example for timeline by removing the package line, removing the class line, removing the "constructor" opening, and deleting any "private" or "public" words. Also delete any extra closed braces at the end of the script, and the end of where the constructor function was.

In this example's case there is one line that you have to add something, which is where the stage reference is stored. Or you could replace that variable with "stage". In my version below I just put stage into that variable.

So, make a new movieclip on your FLA stage, go into the movieclip, and put this script into frame 1's timeline, then do a test movie:

import flash.display.MovieClip;

import flash.events.*;

import flash.display.Stage;

var stageRef:Stage = stage;

var speed:Number = 10;

x = stageRef.stageWidth / 2;

y = stageRef.stageHeight / 2;

this.stageRef = stageRef;

addEventListener(Event.ENTER_FRAME, loop, false, 0, true);

function loop(e:Event):void {

var yDistance:Number = stageRef.mouseY - y;

var xDistance:Number = stageRef.mouseX - x;

if (Math.sqrt(yDistanceyDistance + xDistancexDistance) < speed) {

x = stageRef.mouseX;

y = stageRef.mouseY;

} else {

var radian:Number = Math.atan2(yDistance,xDistance);

x += Math.cos(radian) * speed;

y += Math.sin(radian) * speed;

rotation = radian * 180 / Math.PI;

}

joeboy_ukAuthor
Inspiring
December 13, 2011

many thanks!!!!

I changed it a little to make it work properly but it's great now

import flash.display.MovieClip;

import flash.events.*;

import flash.display.Stage;

var stageRef:Stage=stage

var speed:Number = 10;

x = stageRef.stageWidth / 2;

y = stageRef.stageHeight / 2;

this.stageRef = stageRef;

addEventListener(Event.ENTER_FRAME, loop, false, 0, true);

function loop(e:Event):void

{

          var yDistance:Number = stageRef.mouseY - y;

          var xDistance:Number = stageRef.mouseX - x;

          var radian:Number = Math.atan2(yDistance,xDistance);

          rotation = radian * 180 / Math.PI;

          x -=  (x - stageRef.mouseX) / speed;

          y -=  (y - stageRef.mouseY) / speed;

}