Hey guys, so I thought I had this down but I am still having small issues… And I am talking about accessing the value of a variable in one class from a another. For what I am working on, I am trying to access the boolean value (true/false) of a variable in my enemy class from a conditional statement in my player class.
Everything runs, but when I run a trace from my player class, it never changes from false, when I trace from my enemy class it changes true/false like it is supposed to.
In my player class I basically have
if(enemy.isHit == false) { do something } if(enemy.isHit == true) { do something else }
In the player class I created enemy as a new theEnemy and I have the isHit boolean variable in theEnemy class as public. I was just wondering if there was something that I was missing fundamentally which would explain why my instance of theEnemy class in my player class is not getting anything other than false, even though the value in theEnemy class is most definitely changing.
Here is the Enemy.as in case it is needed to formulate speculation:
package entities {
import net.flashpunk.Entity;
import net.flashpunk.graphics.Image;
import net.flashpunk.FP;
import net.flashpunk.graphics.Spritemap;
import worlds.theWorld
import net.flashpunk.World;
public class theEnemy extends Entity
{
private var power:Number = 2;
public var isHit:Boolean = false;
private var sprEnemy:Spritemap = new Spritemap(ENEMY, 40, 40);
[Embed(source = '../assets/enemy.png')]
public static const ENEMY:Class;
public function theEnemy(posX:int, posY:int)
{
graphic = sprEnemy;
sprEnemy.add("chomp", [0, 1, 2], 5, true);
setHitbox(40,40);
type="enemy";
x = posX * 40;
y = posY * 40;
sprEnemy.play("chomp");
}
override public function update():void
{
if (!isHit)//if false move left
{
x -= power;
}
if (isHit)//if true move right
{
x += power;
}
if (collide("wall", x - 5, y) && isHit == false)
{
isHit = true;
}
if (collide("wall", x + 5, y) && isHit == true)
{
isHit = false;
}
trace("isHit from the enemy class: " + isHit);
super.update();
}
}
}
Thanks guys!