Bizarre Issues in FlashDevelop
I have some issues in a game I'm making in Action Script 3. I'm making it with knowledge from every tutorial I've ever read by using FlashDevelop. I've come across several errors in this code, however, that prevent me from compiling. Here are the only two AS files it has so far:
Main.as
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.utils.Timer;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class Main extends Sprite
{
public var gameTimer:Timer
public var LeftisPressed:Boolean;
public var RightisPressed:Boolean;
public var jumper = new Jumper;
public function Main()
{
gameTimer = new Timer( 40 )
gameTimer.addEventListener(TimerEvent开发者_StackOverflow.TIMER, onTick);
RightisPressed = false;
LeftisPressed = false;
addEventListener( Event.ADDED_TO_STAGE, OnAddToStage() );
jumper = new Jumper();
addChild( jumper );
}
public function OnAddtoStage() :void
{
stage.addEventListener( KeyboardEvent.KEY_DOWN, onKeyPress );
stage.addEventListener( KeyboardEvent.KEY_UP, onKeyRelease );
}
public function onTick (timerEvent: TimerEvent) :void
{
if (LeftisPressed)
{
jumper.Left();
}
if (RightisPressed)
{
jumper.Right();
}
}
public function onKeyPress (keyboardEvent: KeyboardEvent) :void
{
if (KeyboardEvent.keyCode == Keyboard.LEFT )
{
LeftisPressed = true;
}
if (KeyboardEvent.keyCode == Keyboard.RIGHT )
{
RightisPressed = true;
}
}
public function onKeyRelease(keyboardEvent: KeyboardEvent) :void
{
if (KeyboardEvent.keyCode == Keyboard.LEFT )
{
LeftisPressed = false;
}
if (KeyboardEvent.keyCode == Keyboard.RIGHT )
{
LeftisPressed = false;
}
}
}
}
Jumper.as
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.utils.Timer;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
/**
* ...
* @author Timothy Bumpus
*/
public class Jumper extends Sprite
{
public function Jumper()
{
var square:Sprite = new Sprite();
addChild(square);
square.graphics.lineStyle(3,0x00ff00);
square.graphics.beginFill(0x0000FF);
square.graphics.drawRect(0,0,100,100);
square.graphics.endFill();
square.x = 250
square.y = 350
}
public function Left()
{
x = x - 3
}
public function Right()
{
x = x + 3
}
}
}
(In case it matters to you, it's called jumper because i'll eventually make it jump.)
The idea is to make a sprite move left and right to the corresponding keys. But when I try and debug it, it gives me 5 errors, "col: 44 Error: Call to a possibly undefined method OnAddToStage." and "col: 22 Error: Access of possibly undefined property keyCode through a reference with static type Class." for each time I mention keyCode. After searching the internet, my only conclusion is it has something to do with the Flex SDK FlashDevelop uses, but I can't find any specific help for the error in that context.
I can't be the only person who's had this problem. Any ideas?
On this line in your constructor you're making a common actionscript beginner mistake:
addEventListener( Event.ADDED_TO_STAGE, OnAddToStage() );
You're passing the result of the OnAddToStage function to addEventListener, in this case that is void
, so you're not passing anything. What you want to do is pass the actual function like this:
// note the missing paranthesis
addEventListener( Event.ADDED_TO_STAGE, OnAddToStage );
Down in your keyhandler you are doing a similar mistake, KeyboardEvent
is the class, keyboardEvent
(with a lowercase k) is the actual event instance, so swap those out like this:
if (keyboardEvent.keyCode == Keyboard.LEFT )
{
LeftisPressed = true;
}
The key-constants are static properties on the class, so they should stay they way they are.
You will also notice that the actionscript convention is to use lowerCamelCase for functions and variables and save the UpperCamelCase for classes. This is not required, but as classes and functions are objects too it can get mightily confusing if you're not careful.
The error messages are quite descriptive.
The first one is complaining about this line:
addEventListener( Event.ADDED_TO_STAGE, OnAddToStage() );
You are calling the OnAddToStage
method when really you want to reference it, like this:
addEventListener( Event.ADDED_TO_STAGE, OnAddToStage );
addEventListener
is expecting a method, so unless OnAddToStage
returns a method (which it doesn't in this case), an error results when you call it instead of passing it in.
The second error is because you are using KeyboardEvent.keyCode
(KeyboardEvent
is the class) instead of keyboardEvent.keyCode
(your variable).
These issues have nothing to do with FlashDevelop.
精彩评论