Copy link to clipboard
Copied
I trying to learn classes so I decided to convert a file I had.. it didn't take long before I had trouble
this is my orignal file in green it works. I am working directly to the frame I have no main AS file. the only AS file is a replacement of player which is below the green. The green is the original working file
var moveSpeed:Number = 5;
var targetX:Number = 0;
var targetY:Number = 0;
stage.addEventListener(MouseEvent.CLICK, click);
function click(e:MouseEvent):void {
targetX = mouseX;
targetY = mouseY;
addEventListener(Event.ENTER_FRAME, update);
}
function update(e:Event):void {
var p1:Point = new Point(player.x,player.y)
var p2:Point = new Point(targetX,targetY)
trace(Point.distance(p1,p2))
if(Point.distance(p1,p2)<200){
if (Math.abs(targetX - player.x) < moveSpeed) {
// reached target
player.x = targetX;
} else if (targetX > player.x) {
player.gotoAndStop("rightwalk");
player.x += moveSpeed;
} else {
// move left
player.gotoAndStop("leftwalk");
player.x -= moveSpeed;
}
if (Math.abs(targetY - player.y) < moveSpeed) {
// reached target
player.y = targetY;
} else if (targetY > player.y) {
player.gotoAndStop("frontwalk");
player.y += moveSpeed;
} else {
player.gotoAndStop("backwalk");
player.y -= moveSpeed;
}
}
}
this is my class file of player(movieclip) the part in blue works, the part in red does not work, player moves but not right
if the first if statement is active in the update function player moves but hardly. if it is gone player moves but not as accurate and does not stop. maybe I am missing an import
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.Event
import flash.geom.Point;
public class linkall extends MovieClip {
public var targetX:Number = 0
public var targetY:Number = 0
public var moveSpeed:Number = 5
public function linkall() {
// constructor code
//trace("hi")
stop();
listeners()
}
private function listeners() {
stage.addEventListener(MouseEvent.CLICK, click);
}
public function click(e:MouseEvent):void {
targetX = mouseX;
targetY = mouseY;
//trace(targetX)
addEventListener(Event.ENTER_FRAME, update);
}
public function update(e:Event):void {
var p1:Point = new Point(this.x,this.y)
var p2:Point = new Point(targetX,targetY)
trace(Point.distance(p1,p2))
//if(Point.distance(p1,p2)<200){
if (Math.abs(targetX - this.x) < moveSpeed) {
// reached target
this.x = targetX;
} else if (targetX > this.x) {
this.gotoAndStop("rightwalk");
this.x += moveSpeed;
} else {
// move left
this.gotoAndStop("leftwalk");
this.x -= moveSpeed;
}
if (Math.abs(targetY - this.y) < moveSpeed) {
// reached target
this.y = targetY;
} else if (targetY > this.y) {
this.gotoAndStop("frontwalk");
this.y += moveSpeed;
} else {
this.gotoAndStop("backwalk");
this.y -= moveSpeed;
}
}
}
//}
}
you need to use an added_to_stage listener. in the listener function you can reference the stage.
Copy link to clipboard
Copied
is linkall the class of a movieclip that's on stage?
Copy link to clipboard
Copied
yes he is on the stage
Copy link to clipboard
Copied
you helped me figure it out I set player to 0x 0y and he worked
Copy link to clipboard
Copied
great.
Copy link to clipboard
Copied
I took him off the stage to see if I could put him center on stage and now I get this error
I added him with basic addchild on the timeline
var mc:MovieClip = new linkall();
addChild(mc);
then I tried in the main AS file under the public class that linked to main stage properties to do the same thing
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at linkall/listeners()
at linkall()
at guymoving()
obviously I am missing how to link them together
and I don't really understand why the first scenario where the code is typed from the main timeline I can move my player anywhere
and he works. I am guessing it has something to do with time frame and when it loads.
Copy link to clipboard
Copied
var mc:linkall = new linkall();
addChild(mc);
ialso tried that
Copy link to clipboard
Copied
you need to use an added_to_stage listener. in the listener function you can reference the stage.
Copy link to clipboard
Copied
Thank you
Copy link to clipboard
Copied
you're welcome.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now