electriczap4
03-30-2011, 12:34 AM
Sorry if this is a really newbie question, I'm honestly new to scripting flash games and am self-taught, so please feel free to correct any beginner mistakes and please, please talk slowly :p.
I'm making a little flash game to get me used to AS3, and I have a set of booleans in the engine.as file. The variables are set to public. I try to call the variables in my playerShip class, in the loop to control the ship (up, down, left, right)
However, when I compile it says I'm trying to access a possibly undefined property through a reference to a static type class.
My code is as below:
// engine.as
package
{
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
public class engine extends MovieClip
{
//Key booleans
public var leftKey:Boolean = false
public var rightKey:Boolean = false
public var upKey:Boolean = false
public var downKey:Boolean = false
public var shootKey:Boolean = false
public var ship:playerShip = new playerShip()
public function engine()
{
// constructor code
stage.addChild(ship)
ship.x = stage.stageWidth/2;
ship.y = stage.stageHeight/2;
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler)
//stage.addEventListener
}
public function keyDownHandler(e:KeyboardEvent):void
{
if(e.keyCode == 37)
{
leftKey = true
}
if(e.keyCode == 38)
{
upKey = true
}
if(e.keyCode == 39)
{
rightKey = true
}
if(e.keyCode == 40)
{
downKey = true
}
if(e.keyCode == 32)
{
shootKey = true
}
}
public function keyUpHandler(e:KeyboardEvent):void
{
if(e.keyCode == 37)
{
leftKey = false
}
if(e.keyCode == 38)
{
upKey = false
}
if(e.keyCode == 39)
{
rightKey = false
}
if(e.keyCode == 40)
{
downKey = false
}
if(e.keyCode == 32)
{
shootKey = false
}
}
}
}
---------------------
//playerShip.as
package
{
import flash.display.MovieClip;
import flash.events.Event;
public class playerShip extends MovieClip
{
private var moveSpeed:Number = 10
public function playerShip()
{
// constructor code
addEventListener(Event.ENTER_FRAME, loop)
}
private function loop(Events:Event)
{
//Ship movement
if (engine.upKey)
{
this.y -= moveSpeed
}
if (engine.downKey)
{
this.y += moveSpeed
}
if (engine.leftKey)
{
this.x -= moveSpeed
}
if (engine.rightKey)
{
this.x += moveSpeed
}
}
}
}
Again, I'm really sorry if this is a stupid question.
I'm making a little flash game to get me used to AS3, and I have a set of booleans in the engine.as file. The variables are set to public. I try to call the variables in my playerShip class, in the loop to control the ship (up, down, left, right)
However, when I compile it says I'm trying to access a possibly undefined property through a reference to a static type class.
My code is as below:
// engine.as
package
{
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
public class engine extends MovieClip
{
//Key booleans
public var leftKey:Boolean = false
public var rightKey:Boolean = false
public var upKey:Boolean = false
public var downKey:Boolean = false
public var shootKey:Boolean = false
public var ship:playerShip = new playerShip()
public function engine()
{
// constructor code
stage.addChild(ship)
ship.x = stage.stageWidth/2;
ship.y = stage.stageHeight/2;
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler)
//stage.addEventListener
}
public function keyDownHandler(e:KeyboardEvent):void
{
if(e.keyCode == 37)
{
leftKey = true
}
if(e.keyCode == 38)
{
upKey = true
}
if(e.keyCode == 39)
{
rightKey = true
}
if(e.keyCode == 40)
{
downKey = true
}
if(e.keyCode == 32)
{
shootKey = true
}
}
public function keyUpHandler(e:KeyboardEvent):void
{
if(e.keyCode == 37)
{
leftKey = false
}
if(e.keyCode == 38)
{
upKey = false
}
if(e.keyCode == 39)
{
rightKey = false
}
if(e.keyCode == 40)
{
downKey = false
}
if(e.keyCode == 32)
{
shootKey = false
}
}
}
}
---------------------
//playerShip.as
package
{
import flash.display.MovieClip;
import flash.events.Event;
public class playerShip extends MovieClip
{
private var moveSpeed:Number = 10
public function playerShip()
{
// constructor code
addEventListener(Event.ENTER_FRAME, loop)
}
private function loop(Events:Event)
{
//Ship movement
if (engine.upKey)
{
this.y -= moveSpeed
}
if (engine.downKey)
{
this.y += moveSpeed
}
if (engine.leftKey)
{
this.x -= moveSpeed
}
if (engine.rightKey)
{
this.x += moveSpeed
}
}
}
}
Again, I'm really sorry if this is a stupid question.