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

Classes and Functions

Guest
Dec 25, 2015 Dec 25, 2015

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!


TOPICS
ActionScript
782
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 , Dec 25, 2015 Dec 25, 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_

...
Translate
Community Expert ,
Dec 25, 2015 Dec 25, 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!


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
Guest
Dec 26, 2015 Dec 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

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 ,
Dec 26, 2015 Dec 26, 2015

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

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
Guest
Dec 26, 2015 Dec 26, 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

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 ,
Dec 26, 2015 Dec 26, 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

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
Guest
Dec 27, 2015 Dec 27, 2015

Gotcha! I see what I did wrong

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 ,
Dec 27, 2015 Dec 27, 2015
LATEST

(p.s when using the adobe forums, please mark helpful/correct responses, if there are any.)

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