0

Hey guys sorry if this is a re-post i checked for the answer through other sites as well as this but the information wasn't sufficient plus I'm still learning a lot.

But my question is that i have my engine class which is called keepFocusEngine and another class called mcStar i haven't ran across this problem so i don't really understand how to do it but i want to access a Integer variable from the mcStar through my keepFocusEngine. So in my mcStar class i have a integer variable called private var speed:Number; which holds this in it speed = 2 + Math.random()*2;. I want to change the speed variable to this speed = 10 + Math.random()*2; from my keepFocusEngine through this function:

private function shootPlayerObject(e:TouchEvent):void 
    {
        if (e.type == TouchEvent.TOUCH_TAP)
        {
            btnShootPlayer = true;
        }else
        {
            btnShootPlayer = false;
        }
    }

so if the btnShootPlayer = true; i want the speed = 10 + Math.random()*2; to be called on.

Sorry if i confused anyone I'm kind of confusing myself haha. But do you understand what i am trying to accomplish?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Nathan
  • 536
  • 4
  • 21
  • You named it `private`, this means you can't change it while it's private. It'll be better if you write a function for your ship that would be called at a specific event, which will in turn alter its speed. – Vesper Apr 16 '13 at 06:18

1 Answers1

0

If you want to do it neatly, you can write getter/setter functions in your keepFocusEngine class. Then you can leave your speed variable to private. The getter and setter have to be public, ofcourse, to be reached outside the class.

In your keepFocusEngine, add the following:

public function GetSpeed() : Number
{
    return speed;
}

public function SetSpeed(newspeed:Number)
{
    speed = newspeed;
}

Then you can change your provided code into:

private function shootPlayerObject(e:TouchEvent):void 
{
    if (e.type == TouchEvent.TOUCH_TAP)
    {
        btnShootPlayer = true;
        keepFocusEngine.SetSpeed(10 + Math.random()*2);
    }else
    {
        btnShootPlayer = false;
    }
}

If at any point you'd like to know the speed, you can use keepFocusEngine.GetSpeed(). This makes it easier to program since you have some insight in your classpath. Also, when a getter or setter is called, you can do whatever you want in that function. If it seems easier for you to always return the speed*10 for example, a getter can return speed*10;.

EDIT: I'd like to refer to this question: Why prefer Properties to public variables?

Community
  • 1
  • 1
Joetjah
  • 6,292
  • 8
  • 55
  • 90
  • AS3 actually has support for true getters and setters. `public function set varName( value:DataType ):void {}` and `public function get varName():DataType {}` – Josh Apr 16 '13 at 16:16
  • Thank you Joetjah for the response ill try it out later today and let you know if it worked right! – Nathan Apr 16 '13 at 17:16
  • @Apocalyptic what do you mean "true" getters and setters? How would your code above work? – Nathan Apr 16 '13 at 17:17
  • @user2233653 You would access it just as you would a normal variable. So... `public function set varName( value:DataType ):void {}` and `public function get varName():DataType {}` in `ClassName`. You then declare a ClassName object and access varName like this: `var obj:ClassName = new ClassName(); obj.varName = "test"`. See [Step 13 in this Article](http://active.tutsplus.com/tutorials/actionscript/as3-101-oop-inheritance-setters-getters-basix/) – Josh Apr 16 '13 at 17:22
  • @JoetJah I tried your method but i keep getting an error "possibly undefined method through a static type class. Also wouldn't i put the getter/setter inside the mcStar class? since thats where the the speed variable is? sorry i think i left that information out. Then i would add this inside the keepFocusEngine(); mcStar.SetSpeed(10 + Math.random()*2); – Nathan Apr 16 '13 at 22:01
  • You are correct! I've read the facts wrong. The getter and setter are indeed in the class your variable is in. `private` makes sure only methods from the same class can reach that variable. If you create public getters and setters, you can access the private variable (through a method then). – Joetjah Apr 18 '13 at 06:40
  • http://stackoverflow.com/questions/737290/why-prefer-properties-to-public-variables I'd like to refer to this question as well. – Joetjah Apr 18 '13 at 08:14
  • Thanks for the information and links. Really appreciate it. Already read most of it. But im not sure what is going wrong here. I did all the steps and corrected the class issue but i still get this error: keepFocusEngine.as:574: 1061: Call to a possibly undefined method SetSpeed through a reference with static type Class. – Nathan Apr 18 '13 at 22:14
  • When you call `SetSpeed`, make sure you have the parameter in it. Also, make sure you have an instance of the object. So, for example, you run: `keepFocusEngine.getInstance().SetSpeed(10 + Math.random()*2);` How does that work out? – Joetjah Apr 19 '13 at 06:39