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

problems converting to class object movement.

Community Beginner ,
Sep 20, 2018 Sep 20, 2018

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;

    }

}

}

//}

}

TOPICS
ActionScript
415
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 , Sep 21, 2018 Sep 21, 2018

you need to use an added_to_stage listener.  in the listener function you can reference the stage.

Translate
Community Expert ,
Sep 20, 2018 Sep 20, 2018

is linkall the class of a movieclip that's on stage?

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, 2018 Sep 20, 2018

yes he is on the stage

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, 2018 Sep 20, 2018

you helped me figure it out I set player to 0x 0y and he worked

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 ,
Sep 20, 2018 Sep 20, 2018

great.

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, 2018 Sep 20, 2018

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. 

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, 2018 Sep 20, 2018

var mc:linkall = new linkall();

addChild(mc);

ialso tried 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
Community Expert ,
Sep 21, 2018 Sep 21, 2018

you need to use an added_to_stage listener.  in the listener function you can reference the stage.

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 22, 2018 Sep 22, 2018

Thank you

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 ,
Sep 22, 2018 Sep 22, 2018
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