PDA

View Full Version : Can't access public variable?


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.

estudio56
03-30-2011, 03:39 AM
First of all, get into the habit of naming your classes with title case (e.g. Engine.as, PlayerShip.as).

Your loop function in PlayerShip doesn't make sense. Why check onEnterFrame for a key event, when you can listen to it.

On your Engine.as change:


stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler)


for


ship.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler)


Then remove the loop() and eventListener from your PlayerShip class.

And on your Engine.as class, your keyUpHandler() would look something like this:


public function keyUpHandler(e:KeyboardEvent):void
{
var target:playerShip = e.currentTarget as playerShip;

switch(e.keyCode)
{
case Keyboard.LEFT:
target.x -= moveSpeed;
break;

case Keyboard.RIGHT:
target.x += moveSpeed;
break;

case Keyboard.UP:
target.y -= moveSpeed;
break;

case Keyboard.DOWN:
target.y += moveSpeed;
break;

case Keyboard.SPACE:
// handle fire;
break;
}
}


Don't forget to move "private var moveSpeed:Number = 10" from PlayerShip to Engine, and also import the Keyboard class.

In synthesis, your PlayerShip class should only worry about rendering the visual elements and dispatching events. Let another class mediate the PlayerShip and handle it's events.

Hope this helps.

Seb.

electriczap4
03-30-2011, 08:14 PM
First of all, get into the habit of naming your classes with title case (e.g. Engine.as, PlayerShip.as).

Your loop function in PlayerShip doesn't make sense. Why check onEnterFrame for a key event, when you can listen to it.

On your Engine.as change:


stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler)


for


ship.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler)


Then remove the loop() and eventListener from your PlayerShip class.

And on your Engine.as class, your keyUpHandler() would look something like this:


public function keyUpHandler(e:KeyboardEvent):void
{
var target:playerShip = e.currentTarget as playerShip;

switch(e.keyCode)
{
case Keyboard.LEFT:
target.x -= moveSpeed;
break;

case Keyboard.RIGHT:
target.x += moveSpeed;
break;

case Keyboard.UP:
target.y -= moveSpeed;
break;

case Keyboard.DOWN:
target.y += moveSpeed;
break;

case Keyboard.SPACE:
// handle fire;
break;
}
}


Don't forget to move "private var moveSpeed:Number = 10" from PlayerShip to Engine, and also import the Keyboard class.

In synthesis, your PlayerShip class should only worry about rendering the visual elements and dispatching events. Let another class mediate the PlayerShip and handle it's events.

Hope this helps.

Seb.
So what exactly does
var target:playerShip = e.currentTarget as playerShip;

do?

Also, the loop is to check every frame if the key is down, and move if it is, so I have a smooth motion.

Also, what does the switch statement do? I'm not too familiar with programming, and just learning.


Also, so should I make another class-type thing to control the movement of my ship, I'm not really clear on what you meant...


Thanks for all the help, I really do appreciate it.