Skip to main content
December 26, 2015
Answered

Classes and Functions

  • December 26, 2015
  • 2 replies
  • 850 views

First post! Just learning ActionScript 3 so please be gentle and treat me as if I am very dumb.


Alright, wanted to experiment and create a character that moves around, I have the main class and my character class. I have done this once before and had it working but I want to try and keep everything nice and tight. So I tried keeping everything that I want my character to do, inside his class.as file. Then in my main.as file all I would need to do is call him to the stage and remove him when im done with him. Im not getting any errors but nothing is happening at all.


P.S.


I dont know if I am supposed to use the "Insert" function when posting code, and if so there is no "AS3" option, so imma just do it like this.


(Main.as)

package {

  import flash.display.MovieClip;

  public class Main extends MovieClip {

  public function Main () {

  var player : mcCharacter = new mcCharacter();

  player.x = 20;

  player.y = 410;

  addChild (player);

  }

  }

}

(end)

(mcCharacter.as)

package {

  import flash.display.MovieClip;

  import flash.events.*;

  public class mcCharacter extends MovieClip {


  public var moveRight:Boolean = false;

  public function mcCharacter () {

  addEventListener (KeyboardEvent.KEY_DOWN, keyboardPress);

  addEventListener (Event.ENTER_FRAME, FrameEvents);

  }

  public function FrameEvents (event:Event):void {

  if (moveRight == false) {

  }

  if (moveRight == true) {

  x = x + 5;

  }

  }

  public function keyboardPress (event:KeyboardEvent):void {

  trace (event.keyCode);

  if (event.keyCode == 68) {

  moveRight = true;

  } else {

  moveRight = false;

  }

  }

  }

}

(end)

I dont understand whats happening.

ALSO!

I keep hearing that everyone wants flash to go away, and in early 2016 they will release Adobe Animate CC, will they be getting rid of Actionscript? Am I wasting my time doing this? I got interested in Flash because I see alot of content using flash, I would like to become a game developer using Adobe Flash but am I wasting my time?

Thank you anyone who takes the time to read this and gives me feedback, I appreciate you!


This topic has been closed for replies.
Correct answer kglad

try:



(Main.as)

package {

  import flash.display.MovieClip;

  public class Main extends MovieClip {

  public function Main () {

  var player : mcCharacter = new mcCharacter();

  player.x = 20;

  player.y = 410;

  addChild (player);

  }

  }

}

(end)

(mcCharacter.as)

package {

  import flash.display.MovieClip;

  import flash.events.*;

  public class mcCharacter extends MovieClip {


  public var moveRight:Boolean = false;


  public function mcCharacter () {

this.addEventListener(Event.ADDED_TO_STAGE,addedF);

  addEventListener (Event.ENTER_FRAME, FrameEvents);

  }

  public function FrameEvents (event:Event):void {

  if (moveRight == false) {

  }

  if (moveRight == true) {

  x = x + 5;

  }

  }

  public function keyboardPress (event:KeyboardEvent):void {

  trace (event.keyCode);

  if (event.keyCode == 68) {

  moveRight = true;

  } else {

  moveRight = false;

  }

  }

private function addedF(e:Event):void{

  this.stage.addEventListener (KeyboardEvent.KEY_DOWN, keyboardPress);

}

  }

}

(end)

I dont understand whats happening.

ALSO!

I keep hearing that everyone wants flash to go away, and in early 2016 they will release Adobe Animate CC, will they be getting rid of Actionscript? Am I wasting my time doing this? I got interested in Flash because I see alot of content using flash, I would like to become a game developer using Adobe Flash but am I wasting my time?

//as3 is not going away any time soon because it's still used to create android and ios apps even if it's not used as frequently for web apps. 

Thank you anyone who takes the time to read this and gives me feedback, I appreciate you!


2 replies

December 27, 2015

package {

  import flash.display.MovieClip;

  import flash.events.*;

  public class mcCharacter extends MovieClip {

  public var moveRight:Boolean = false;

  public function mcUser () {

  this.stage.addEventListener (Event.ADDED_TO_STAGE, addedF); <------- LINE 13

  addEventListener (KeyboardEvent.KEY_DOWN, keyboardPress);

  addEventListener (Event.ENTER_FRAME, FrameEvents);

  }

  public function FrameEvents (event:Event):void {

  if (moveRight == false) {

  }

  if (moveRight == true) {

  x = x + 5;

  }

  }

  public function keyboardPress (event:KeyboardEvent):void {

  trace (event.keyCode);

  if (event.keyCode == 68) {

  moveRight = true;

  } else {

  moveRight = false;

  }

  }

  private function addedF (e:Event):void {

  this.stage.addEventListener (KeyboardEvent.KEY_DOWN, keyboardPress);

  }

  }

}

And then ALSO on the MAIN.as

package {

  import flash.display.MovieClip;

  public class Main extends MovieClip {

  public function Main () {

  var player:mcCharacter = new mcCharacter();<-----------LINE 9

  player.x = 20;

  player.y = 410;

  addChild (player);

  }

  }

}

ERROR 1009

kglad
Community Expert
Community Expert
December 27, 2015

that's not the code i suggested.  you must use an addedtostage listener to reference the stage in that class.

ThunderfootMOB wrote:

package {

  import flash.display.MovieClip;

  import flash.events.*;

  public class mcCharacter extends MovieClip {

  public var moveRight:Boolean = false;

  public function mcUser () {

  this.addEventListener (Event.ADDED_TO_STAGE, addedF); <------- LINE 13

  addEventListener (KeyboardEvent.KEY_DOWN, keyboardPress);

  addEventListener (Event.ENTER_FRAME, FrameEvents);

  }

  public function FrameEvents (event:Event):void {

  if (moveRight == false) {

  }

  if (moveRight == true) {

  x = x + 5;

  }

  }

  public function keyboardPress (event:KeyboardEvent):void {

  trace (event.keyCode);

  if (event.keyCode == 68) {

  moveRight = true;

  } else {

  moveRight = false;

  }

  }

  private function addedF (e:Event):void {

  this.stage.addEventListener (KeyboardEvent.KEY_DOWN, keyboardPress);

  }

  }

}

And then ALSO on the MAIN.as

package {

  import flash.display.MovieClip;

  public class Main extends MovieClip {

  public function Main () {

  var player:mcCharacter = new mcCharacter();<-----------LINE 9

  player.x = 20;

  player.y = 410;

  addChild (player);

  }

  }

}

ERROR 1009

December 27, 2015

Gotcha! I see what I did wrong

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
December 26, 2015

try:



(Main.as)

package {

  import flash.display.MovieClip;

  public class Main extends MovieClip {

  public function Main () {

  var player : mcCharacter = new mcCharacter();

  player.x = 20;

  player.y = 410;

  addChild (player);

  }

  }

}

(end)

(mcCharacter.as)

package {

  import flash.display.MovieClip;

  import flash.events.*;

  public class mcCharacter extends MovieClip {


  public var moveRight:Boolean = false;


  public function mcCharacter () {

this.addEventListener(Event.ADDED_TO_STAGE,addedF);

  addEventListener (Event.ENTER_FRAME, FrameEvents);

  }

  public function FrameEvents (event:Event):void {

  if (moveRight == false) {

  }

  if (moveRight == true) {

  x = x + 5;

  }

  }

  public function keyboardPress (event:KeyboardEvent):void {

  trace (event.keyCode);

  if (event.keyCode == 68) {

  moveRight = true;

  } else {

  moveRight = false;

  }

  }

private function addedF(e:Event):void{

  this.stage.addEventListener (KeyboardEvent.KEY_DOWN, keyboardPress);

}

  }

}

(end)

I dont understand whats happening.

ALSO!

I keep hearing that everyone wants flash to go away, and in early 2016 they will release Adobe Animate CC, will they be getting rid of Actionscript? Am I wasting my time doing this? I got interested in Flash because I see alot of content using flash, I would like to become a game developer using Adobe Flash but am I wasting my time?

//as3 is not going away any time soon because it's still used to create android and ios apps even if it's not used as frequently for web apps. 

Thank you anyone who takes the time to read this and gives me feedback, I appreciate you!


December 26, 2015

Not working, getting:

TypeError: Error #1009: Cannot access a property or method of a null object reference.

  at mcCharacter().as:13]

  at Main()Main.as:9]

I read up on the ADDED_TO_STAGE event, seems useful but I at first I wasnt getting any errors, just nothing was happening

kglad
Community Expert
Community Expert
December 26, 2015

copy mcCharacter.as that triggers that error and indicate which is line 13.