Jump to content

? servers

? players online

NEED HELP WITH ACTIONSCRIPT 3.0!

Recommended Posts


  • Content Count:  2686
  • Joined:  03/22/08
  • Status:  Offline

http://www.swfupload.com/view/151052.htm

 

SpaceTowers.as = main screen

 

package {
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.display.MovieClip;
import flash.ui.Mouse;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.Event;


public class SpaceTowers extends MovieClip {
	public var player:Player;
	public var useMouseControl:Boolean;
	public var downKeyIsBeingPressed:Boolean;
	public var upKeyIsBeingPressed:Boolean;
	public var leftKeyIsBeingPressed:Boolean;
	public var rightKeyIsBeingPressed:Boolean;
	

	public function SpaceTowers() {
		downKeyIsBeingPressed = false;
		upKeyIsBeingPressed = false;
		leftKeyIsBeingPressed = false;
		rightKeyIsBeingPressed = false;
		player = new Player;
		addChild(player);
		player.x = 200;
		player.y = 200;

		addEventListener( Event.ADDED_TO_STAGE, onAddToStage );
		addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
	}//end function SpaceTowers



	public function onAddToStage( event:Event ):void {
		stage.addEventListener( KeyboardEvent.KEY_DOWN, onKeyPress );
		stage.addEventListener( KeyboardEvent.KEY_UP, onKeyRelease );
	}//end onAddToStage function
	public function onKeyPress( keyboardEvent:KeyboardEvent ):void {
		if ( keyboardEvent.keyCode == Keyboard.DOWN ) {
			downKeyIsBeingPressed = true;

		} else if ( keyboardEvent.keyCode == Keyboard.UP ) {
			upKeyIsBeingPressed = true;
		} else if ( keyboardEvent.keyCode == Keyboard.LEFT ) {
			leftKeyIsBeingPressed = true;
		} else if ( keyboardEvent.keyCode == Keyboard.RIGHT ) {
			rightKeyIsBeingPressed = true;
		}
	}//onKeyPress
	public function onKeyRelease( keyboardEvent:KeyboardEvent ):void {
		if ( keyboardEvent.keyCode == Keyboard.DOWN ) {
			if ( keyboardEvent.keyCode == Keyboard.DOWN ) {
				downKeyIsBeingPressed = false;

			} else if ( keyboardEvent.keyCode == Keyboard.UP ) {
				upKeyIsBeingPressed = false;
			} else if ( keyboardEvent.keyCode == Keyboard.LEFT ) {
				leftKeyIsBeingPressed = false;
			} else if ( keyboardEvent.keyCode == Keyboard.RIGHT ) {
				rightKeyIsBeingPressed = false;
			}
		}
	}//onKeyRelease
	public function loop(e:Event):void {

		if (upKeyIsBeingPressed) {

			player.liigu();
		} else {
			player.peatu();

		}
		if (rightKeyIsBeingPressed) {

			player.keeraParemale();
		} else if (leftKeyIsBeingPressed) {
		player.keeraVasakule();
	}
	if ( player.x {
player.x = player.width / 2;
}
if ( player.x > 500 - ( player.width / 2 ) )
{
player.x = 500 - ( player.width / 2 );
}
if ( player.y {
player.y = player.height / 2;
}
if ( player.y > 400 - ( player.height / 2 ) )
{
player.y = 400 - ( player.height / 2 );
}
}
}//end class
}

 

Player.as = player

 

package {
import flash.display.MovieClip;
import flash.display.Stage;
import flash.events.Event;

public class Player extends MovieClip {
	private var speed:Number = 0.3;
	private var rotateSpeed:Number = 5;
	private var vx:Number = 0;
	private var vy:Number = 0;
	private var friction:Number = 0.95;


	public function Ship():void {


	}
	
	public function liigu() {
		vy += Math.sin(degreesToRadians(rotation)) * speed;
		vx += Math.cos(degreesToRadians(rotation)) * speed;

		y += vy;
		x += vx;

	}
	public function peatu() {
		vy *= friction;
		vx *= friction;

		y += vy;
		x += vx;


	}
	public function keeraParemale() {
		rotation += rotateSpeed;
	}
	public function keeraVasakule() {
		rotation -= rotateSpeed;
	}
	public function degreesToRadians(degrees:Number):Number {
		return degrees * Math.PI / 180;
	}

}

}

 

The Document Class:

 

package {
import flash.display.MovieClip;
public class DocumentClass extends MovieClip {
	public var playScreen:SpaceTowers;
	public function DocumentClass() {
		playScreen = new SpaceTowers();
		playScreen.x = 0;
		playScreen.y = 0;
		addChild( playScreen );
	}
}
}

 

Problem:

After pressing any arrow key, the game stops responding to key input. Wtf?

 

Ideas?

Link to comment

  • Content Count:  470
  • Joined:  04/04/10
  • Status:  Offline

                public function onKeyRelease( keyboardEvent:KeyboardEvent ):void {
		if ( keyboardEvent.keyCode == Keyboard.DOWN ) {
			...
		}
	}//onKeyRelease

 

Why is everything enclosed in that? What you're basically doing is saying "If the event is a DOWN key release, check if the event is a LEFT/RIGHT/UP/DOWN key release." What ends up happening is that none of the keys (except for down) are released, so once they are activated they stay in the true state. (Even though DOWN can be set to true, I think your UP key higher priority, so your ship keeps moving forward even if down is true.)

 

I'd imagine this would work:

 

		public function onKeyRelease( keyboardEvent:KeyboardEvent ):void {
		if ( keyboardEvent.keyCode == Keyboard.DOWN ) {
			downKeyIsBeingPressed = false;

		} else if ( keyboardEvent.keyCode == Keyboard.UP ) {
			upKeyIsBeingPressed = false;
		} else if ( keyboardEvent.keyCode == Keyboard.LEFT ) {
			leftKeyIsBeingPressed = false;
		} else if ( keyboardEvent.keyCode == Keyboard.RIGHT ) {
			rightKeyIsBeingPressed = false;
		}
               }

  • Like 1
Link to comment

Reply to Thread

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...