Author Topic: [SOLVED] accessing a variable between classes ?  (Read 1290 times)

luc

  • Member
  • **
  • Posts: 97
  • Karma: +0/-0
  • no ablo anything except french
    • View Profile
    • scribbles
Hi, I'm making a small Arkanoid clone so I can train myself at AS3.
I have a type of brick which upon collision won't die but play a little animation and increment a variable in the PlayState instead.
But while I can access the build in score global variable in the hitTop() function  I can't access my own variable.
I'm passing the variable to the brick in the constructor but that doesn't help.
It seems to be a scope issue, but can't figure out the answer.

this a part of the brick

Code: [Select]
public class BrickIndestruct extends FlxSprite
{
// GFX
[Embed(source = '../../data/Brick_indestructible.png')] private var ImgBrickInd:Class;

//SOUNDS
[Embed(source = '../../data/SoundIndestructBrick.swf', symbol = 'FX_IndestructKillReas.wav')] public var sndIndBrickExplode:Class;

private var _PowerAdd:Number;


public function BrickIndestruct(X:Number,Y:Number,POWERADD:Number):void
{
super(X, Y);
loadGraphic(ImgBrickInd, true, true, 16,8);

height = 8;
width = 16;

fixed = true;

_PowerAdd = POWERADD;

addAnimation("idle", [0], 12);
addAnimation("flashTop", [1, 2, 3, 4,5,4,5,4,5,4,5], 12, false);
addAnimation("flashBottom", [6, 7, 8], 18, false);
addAnimation("death", [12, 13, 14, 15, 16, 17], 35, false);

}

and I put this in hitTop

Code: [Select]
override public function hitTop(Contact:FlxObject, Velocity:Number):void
{
if (Contact is Ball)
{
play("flashTop");
_PowerAdd -= 3;
}
}

so in the PlayState I declare this variable then pass it to the brick but nothing happens. Setting the variables to 'public' or 'static' perhaps ?
I want to affect a power bar and change the size of it using .scale . Could be related to passing a wrong type of variable ?
Thanks for reading ^^
« Last Edit: Thu, Jul 22, 2010 by luc »

photonstorm

  • Administrator
  • Key Contributor
  • *****
  • Posts: 1502
  • Karma: +1/-0
    • View Profile
    • Photon Storm
Re: accessing a variable between classes ?
« Reply #1 on: Wed, Jul 21, 2010 »
When you pass a Number data typed variable to a function, it passes the value of that variable, not a reference to the variable itself. If the hitTop method above is in a different class to the PlayState then you're going to have to get/set the value in another way.

One way could be to carry extra vars in the FlxG.scores array. Another is to pass a function to the ball, which could be called by hitTop, and that in itself is a reference to a getter/setter in your PlayState.
http://www.photonstorm.com



"Tell me and I will forget, show me and I might remember, involve me and I will understand" - Confucius

Hectate

  • Active Member
  • ***
  • Posts: 192
  • Karma: +0/-0
    • View Profile
Re: accessing a variable between classes ?
« Reply #2 on: Wed, Jul 21, 2010 »
import the playstate and call PlayState._powerAdd -= 3; might work too?
Patience is a Virtue,
But Haste is my Life.

luc

  • Member
  • **
  • Posts: 97
  • Karma: +0/-0
  • no ablo anything except french
    • View Profile
    • scribbles
Re: accessing a variable between classes ?
« Reply #3 on: Thu, Jul 22, 2010 »
Ok, thanks PhotonStorm :)
I'm using FlxG.levels[0] now and it works.
I bought myself some books on AS3 so I can fill the gaps and have a better understanding of the whole thing.
Ah Hectate I didn't thought of this, is it ok to import a state ?

Hectate

  • Active Member
  • ***
  • Posts: 192
  • Karma: +0/-0
    • View Profile
Re: [SOLVED] accessing a variable between classes ?
« Reply #4 on: Thu, Jul 22, 2010 »
Depending on the size of your playstate it might be an excessively redundant inefficient use of memory, but it works. I've done it before - although it was to access a function, not a variable.
Patience is a Virtue,
But Haste is my Life.

zez

  • Active Member
  • ***
  • Posts: 203
  • Karma: +0/-0
    • View Profile
    • Devlog
Re: [SOLVED] accessing a variable between classes ?
« Reply #5 on: Thu, Jul 22, 2010 »
I wouldnt import the whole playstate, if thats what your asking, but I pass things other class' all the time and thats fairly legit.
I think in the case of just wanting one value, public static var is your friend. I use that ALL THE TIME, mostly to access groups and make spawners and what have you, because I fairly regularly want to do things like make something add or recycle an explosion in the playstate when I call kill, and although I COULD do that in the playstate, it makes more sense to just do it from a sprite.