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

Action Script 3, can someone spot where my code is wrong?

New Here ,
Jan 06, 2015 Jan 06, 2015

I want to have my characters controlled separately, with 'FireBoy' moving using the left, up and right keys and with 'WaterGirl' moving with the a, w and d keys. However upon editing my code somehow WaterGirl now moves with the right key instead of the d key. Please can someone spot where I have made a mistake and advise me on how to resolve the problem.

My code for main.as is:

package

{

  import flash.display.Bitmap;

  import flash.display.Sprite;

  import flash.events.Event;

  import flash.events.KeyboardEvent;

  import flash.events.MouseEvent;

  /**

  * ...

  * @author Harry

  */

  public class Main extends Sprite

  {

  private var leftDown:Boolean = false;

  private var rightDown:Boolean = false;

  private var aDown:Boolean = false;

  private var dDown:Boolean = false;

  private var FireBoy:Hero = new Hero();

  public var StartButton:Go;

  public var WaterGirl:Female = new Female();

  public var Door1:Firedoor;

  public var Door2:Waterdoor;

  public var Fire:Lava;

  public var Water:Blue;

  public var Green:Gem;

  public function Main():void

  {

  if (stage) init();

  else addEventListener(Event.ADDED_TO_STAGE, init);

  }

  public function init(e:Event = null):void

  {

  StartButton = new Go();

  StartButton.x = 100;

  StartButton.y = 5;

  addChild(StartButton);

  StartButton.addEventListener(MouseEvent.CLICK, startgame);

  }

  private function startgame(e:Event = null):void

  {

  removeEventListener(Event.ADDED_TO_STAGE, init);

  // entry point

  removeChild(StartButton);

  FireBoy.y = 495;

  addChild(FireBoy);

  stage.addEventListener(Event.ENTER_FRAME, HerocheckStuff);

  stage.addEventListener(KeyboardEvent.KEY_DOWN, HerokeysDown);

  stage.addEventListener(KeyboardEvent.KEY_UP, HerokeysUp);

  WaterGirl.x = 70;

  WaterGirl.y = 495;

  addChild(WaterGirl);

  stage.addEventListener(Event.ENTER_FRAME, FemalecheckStuff);

  stage.addEventListener(KeyboardEvent.KEY_DOWN, FemalekeysDown);

  stage.addEventListener(KeyboardEvent.KEY_UP, FemalekeysUp);

  Door1 = new Firedoor();

  stage.addChild(Door1);

  Door1.x = 5;

  Door1.y = 62;

  Door2 = new Waterdoor();

  stage.addChild(Door2);

  Door2.x = 100;

  Door2.y = 62;

  Fire = new Lava();

  stage.addChild(Fire);

  Fire.x = 160;

  Fire.y = 570;

  Water = new Blue();

  stage.addChild(Water);

  Water.x = 350;

  Water.y = 160;

  Green = new Gem()

  stage.addChild(Green);

  Green.x = 500;

  Green.y = 100;

  graphics.beginFill(0x804000, 1);

  graphics.drawRect(0, 0, 800, 40);

  graphics.endFill();

  graphics.beginFill(0x804000, 1);

  graphics.drawRect(0, 170, 600, 40);

  graphics.endFill();

  graphics.beginFill(0x804000, 1);

  graphics.moveTo(800, 200);

  graphics.lineTo(800, 700);

  graphics.lineTo(400, 700);

  graphics.lineTo(100, 700);

  graphics.endFill();

  graphics.beginFill(0x804000, 1);

  graphics.drawRect(0, 580, 800, 40);

  graphics.endFill();

  }

  public function Collision():void

  {

  if (WaterGirl.hitTestObject(Fire))

  {

  WaterGirl.x = 70;

  WaterGirl.y = 495;

  }

  if (FireBoy.hitTestObject(Water))

  {

  FireBoy.x = 15;

  FireBoy.y = 495;

  }

  }

  public function HerocheckStuff(e:Event):void

  {

  if (leftDown)

  FireBoy.x -= 5;

  if (rightDown)

  FireBoy.x += 5;

  FireBoy.adjust();

  Collision();

  Check_Border();

  }

  public function HerokeysDown(e:KeyboardEvent):void

  {

  if (e.keyCode == 37)

  leftDown = true;

  if (e.keyCode == 39)

  rightDown = true;

  if (e.keyCode == 38)

  FireBoy.grav = -15;

  Collision();

  Check_Border();

  }

  public function HerokeysUp(e:KeyboardEvent):void

  {

  if (e.keyCode == 37)

  leftDown = false;

  if (e.keyCode == 39)

  rightDown = false;

  }

  public function FemalecheckStuff(e:Event):void

  {

  if (aDown)

  WaterGirl.x -= 5;

  if (rightDown)

  WaterGirl.x += 5;

  WaterGirl.adjust2();

  Collision();

  Check_Border();

  }

  public function FemalekeysDown(e:KeyboardEvent):void

  {

  if (e.keyCode == 65)

  aDown = true;

  if (e.keyCode == 68)

  dDown = true;

  if (e.keyCode == 87)

  WaterGirl.grav = -15;

  Collision();

  Check_Border();

  }

  public function FemalekeysUp(e:KeyboardEvent):void

  {

  if (e.keyCode == 65)

  aDown = false;

  if (e.keyCode == 68)

  dDown = false;

  }

  public function Check_Border():void

  {

  if (FireBoy.x <= 0)

  {

  FireBoy.x = 0;

  }

  else if (FireBoy.x > 750)

  {

  FireBoy.x = 750;

  }

  if (WaterGirl.x <= 0)

  {

  WaterGirl.x = 0;

  }

  else if (WaterGirl.x > 750)

  {

  WaterGirl.x = 750;

  }

     }

  }

}

My code for Hero.as (FireBoy) is:

package 

{

  import flash.display.Bitmap;

  import flash.display.Sprite;

  public class Hero extends Sprite

  {

  [Embed(source="../assets/FireBoy.jpg")]

  private static const FireBoy:Class;

  private var FireBoy:Bitmap;

  public var grav:int = 0;

  public var floor:int = 535;

  public function Hero()

  {

  FireBoy = new Hero.FireBoy();

  scaleX = 0.1;

  scaleY = 0.1;

  addChild(FireBoy);

  }

  public function adjust():void

  {

  this.y += grav;

  if(this.y+this.height/2<floor)

  grav++;

  else

  {

  grav = 0;

  this.y = floor - this.height / 2;

  }

  if (this.x - this.width / 2 < 0)

  this.x = this.width / 2;

  if (this.x + this.width / 2 > 800)

  this.x = 800 - this.width / 2;

  }

  }

}

And finally my code for Female.as (WaterGirl) is:

package 

{

  import flash.display.Bitmap;

  import flash.display.Sprite;

  /**

  * ...

  * @author Harry

  */

  public class Female extends Sprite

  {

  [Embed(source="../assets/WaterGirl.png")]

  private static const WaterGirl:Class;

  private var WaterGirl:Bitmap;

  public var grav:int = 0;

  public var floor:int = 535;

  public function Female()

  {

  WaterGirl = new Female.WaterGirl();

  scaleX = 0.1;

  scaleY = 0.1;

  addChild(WaterGirl);

  }

  public function adjust2():void

  {

  this.y += grav;

  if(this.y+this.height/2<floor)

  grav++;

  else

  {

  grav = 0;

  this.y = floor - this.height / 2;

  }

  if (this.x - this.width / 2 < 0)

  this.x = this.width / 2;

  if (this.x + this.width / 2 > 800)

  this.x = 800 - this.width / 2;

  }

  }

}

TOPICS
ActionScript
333
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

LEGEND , Jan 06, 2015 Jan 06, 2015

You should make use of the trace function to troubleshoot your processing.  Put traces in the different movement function conditionals to see which is being used under which circumstances.  You might consider putting the movement code into one function since they all execute when you process a keyboard event anyways.

Translate
LEGEND ,
Jan 06, 2015 Jan 06, 2015
LATEST

You should make use of the trace function to troubleshoot your processing.  Put traces in the different movement function conditionals to see which is being used under which circumstances.  You might consider putting the movement code into one function since they all execute when you process a keyboard event anyways.

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