# Class variables not being set

**URL:** <https://forum.kirupa.com/t/class-variables-not-being-set/265095>\
**Category:** Uncategorized\
**Created:** [July 3, 2008, 9:50pm UTC](https://forum.kirupa.com/t/class-variables-not-being-set/265095 "2008-07-03T21:50:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![DesignerMichael](https://avatars.discourse-cdn.com/v4/letter/d/898d66/32.png) [@DesignerMichael](https://forum.kirupa.com/u/DesignerMichael)\
**Post date:** [July 3, 2008, 9:50pm UTC](https://forum.kirupa.com/t/class-variables-not-being-set/265095/1 "2008-07-03T21:50:56Z")

</div>

The Speed Variable is not being set in the class init, yet it works later in my timer function… Can you not set variables within classes in AS3? Whats going on…

```auto
function fireProjectile() {
    var myBuggy = gameLevel.getChildByName(Players[myId].childName);
    var Projectile:Cannonball = new Cannonball();
    Projectile.x = myBuggy.Turret.myPoint.gPoint.x - mapScrollX;
    Projectile.y = myBuggy.Turret.myPoint.gPoint.y;

//This is not being set *v*
    Projectile.Speed = 100;

    Projectile.rotation = myBuggy.rotation + myBuggy.Turret.rotation;
    gameLevel.addChild(Projectile);
}

```

My class:

```auto

package projectiles{
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.utils.Timer;
    //Global var class
    import lt.uza.utils.*;
    public class Cannonball extends MovieClip {

        public var Speed:int = new int();

        private var global:Global = Global.getInstance();
        private var xV:Number = new Number();
        private var yV:Number = new Number();
        private var Friction = .90;
        private var Gravity = 1;
        private var projectileControl:Timer = new Timer(20);
        public function Cannonball() {
            xV = -Speed * Math.cos (rotation*Math.PI/180);
            yV = -Speed * Math.sin (rotation*Math.PI/180);
            projectileControl.addEventListener(TimerEvent.TIMER, projectileFunction);
            projectileControl.start();

trace(Speed); //No go! **********

        }
        private function projectileFunction(e:TimerEvent) {

trace(Speed); //Works...
 

            yV = yV + Gravity;
            xV = xV * Friction;
            x += xV;
            y += yV;
            //Set rotation by calculating angle to where the object will be located next frame
            rotation = rotate2Point(Math.atan2((y+yV+Gravity)-y, (x+xV)-x))+180;

            //ADD TRAIL HERE

        }
        private function remove() {
            projectileControl.removeEventListener(TimerEvent.TIMER, projectileFunction);
        }
        private function rotate2Point(r) {
            return r * 180 / Math.PI;
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![TheCanadian](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@TheCanadian](https://forum.kirupa.com/u/TheCanadian)\
**Post date:** [July 3, 2008, 10:32pm UTC](https://forum.kirupa.com/t/class-variables-not-being-set/265095/2 "2008-07-03T22:32:00Z")

</div>

The code in your constructor (including the trace function) is executed immeadiately upon creation of the instance and therefore the code where you set the Speed variable is run after the trace. You should pass the value into your class as an argument of the constructor.

```auto
package projectiles{
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.utils.Timer;
    //Global var class
    import lt.uza.utils.*;
    public class Cannonball extends MovieClip {

        public var Speed:int;

        private var global:Global = Global.getInstance();
        private var xV:Number = new Number();
        private var yV:Number = new Number();
        private var Friction = .90;
        private var Gravity = 1;
        private var projectileControl:Timer = new Timer(20);
        public function Cannonball(s:int) {
			Speed = s;
            xV = -Speed * Math.cos (rotation*Math.PI/180);
            yV = -Speed * Math.sin (rotation*Math.PI/180);
            projectileControl.addEventListener(TimerEvent.TIMER, projectileFunction);
            projectileControl.start();
			
			trace(Speed);
        }
        private function projectileFunction(e:TimerEvent) {
			
			trace(Speed);
			
            yV = yV + Gravity;
            xV = xV * Friction;
            x += xV;
            y += yV;
            //Set rotation by calculating angle to where the object will be located next frame
            rotation = rotate2Point(Math.atan2((y+yV+Gravity)-y, (x+xV)-x))+180;

            //ADD TRAIL HERE

        }
        private function remove() {
            projectileControl.removeEventListener(TimerEvent.TIMER, projectileFunction);
        }
        private function rotate2Point(r) {
            return r * 180 / Math.PI;
        }
    }
}

```

```auto
function fireProjectile() {
    var myBuggy = gameLevel.getChildByName(Players[myId].childName);
    var Projectile:Cannonball = new Cannonball(100);
    Projectile.x = myBuggy.Turret.myPoint.gPoint.x - mapScrollX;
    Projectile.y = myBuggy.Turret.myPoint.gPoint.y;
    Projectile.rotation = myBuggy.rotation + myBuggy.Turret.rotation;
    gameLevel.addChild(Projectile);
}

```

---

<div class="post-metadata">

**Author:** ![DesignerMichael](https://avatars.discourse-cdn.com/v4/letter/d/898d66/32.png) [@DesignerMichael](https://forum.kirupa.com/u/DesignerMichael)\
**Post date:** [July 4, 2008, 12:06am UTC](https://forum.kirupa.com/t/class-variables-not-being-set/265095/3 "2008-07-04T00:06:46Z")

</div>

Ohh, didn’t know you could do that! Nice, thanks!

[quote=TheCanadian;2353357]The code in your constructor (including the trace function) is executed immeadiately upon creation of the instance and therefore the code where you set the Speed variable is run after the trace. You should pass the value into your class as an argument of the constructor.

```
ActionScript Code:
[LEFT]package projectiles[COLOR=#000000]{[/COLOR]
[COLOR=#0000FF]import[/COLOR] flash.[COLOR=#000080]display[/COLOR].*;
[COLOR=#0000FF]import[/COLOR] flash.[COLOR=#000080]events[/COLOR].*;
[COLOR=#0000FF]import[/COLOR] flash.[COLOR=#000080]geom[/COLOR].*;
[COLOR=#0000FF]import[/COLOR] flash.[COLOR=#000080]utils[/COLOR].[COLOR=#000080]Timer[/COLOR];
[COLOR=#808080]*//Global var class*[/COLOR]
[COLOR=#0000FF]import[/COLOR] lt.[COLOR=#000080]uza[/COLOR].[COLOR=#000080]utils[/COLOR].*;
[COLOR=#0000FF]public[/COLOR] [COLOR=#000000] **class** [/COLOR] Cannonball [COLOR=#0000FF]extends[/COLOR] [COLOR=#0000FF]MovieClip[/COLOR] [COLOR=#000000]{[/COLOR]

    [COLOR=#0000FF]public[/COLOR] [COLOR=#000000] **var** [/COLOR] Speed:[COLOR=#0000FF]int[/COLOR];

    [COLOR=#0000FF]private[/COLOR] [COLOR=#000000] **var** [/COLOR] global:Global = Global.[COLOR=#000080]getInstance[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000])[/COLOR];
    [COLOR=#0000FF]private[/COLOR] [COLOR=#000000] **var** [/COLOR] xV:[COLOR=#0000FF]Number[/COLOR] = [COLOR=#000000] **new** [/COLOR] [COLOR=#0000FF]Number[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000])[/COLOR];
    [COLOR=#0000FF]private[/COLOR] [COLOR=#000000] **var** [/COLOR] yV:[COLOR=#0000FF]Number[/COLOR] = [COLOR=#000000] **new** [/COLOR] [COLOR=#0000FF]Number[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000])[/COLOR];
    [COLOR=#0000FF]private[/COLOR] [COLOR=#000000] **var** [/COLOR] Friction = .[COLOR=#000080]90[/COLOR];
    [COLOR=#0000FF]private[/COLOR] [COLOR=#000000] **var** [/COLOR] Gravity = [COLOR=#000080]1[/COLOR];
    [COLOR=#0000FF]private[/COLOR] [COLOR=#000000] **var** [/COLOR] projectileControl:Timer = [COLOR=#000000] **new** [/COLOR] Timer[COLOR=#000000]([/COLOR][COLOR=#000080]20[/COLOR][COLOR=#000000])[/COLOR];
    [COLOR=#0000FF]public[/COLOR] [COLOR=#000000] **function** [/COLOR] Cannonball[COLOR=#000000]([/COLOR]s:[COLOR=#0000FF]int[/COLOR][COLOR=#000000])[/COLOR] [COLOR=#000000]{[/COLOR]

```

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Speed = s;  
xV = -Speed \* [COLOR=#0000FF]Math[/COLOR].[COLOR=#0000FF]cos[/COLOR] COLOR=#000000[/COLOR];  
yV = -Speed \* [COLOR=#0000FF]Math[/COLOR].[COLOR=#0000FF]sin[/COLOR] COLOR=#000000[/COLOR];  
projectileControl.[COLOR=#000080]addEventListener[/COLOR][COLOR=#000000]([/COLOR]TimerEvent.[COLOR=#000080]TIMER[/COLOR], projectileFunction[COLOR=#000000])[/COLOR];  
projectileControl.[COLOR=#0000FF]start[/COLOR]COLOR=#000000[/COLOR];  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;traceCOLOR=#000000[/COLOR];  
[COLOR=#000000]}[/COLOR]  
[COLOR=#0000FF]private[/COLOR] [COLOR=#000000] **function** [/COLOR] projectileFunctionCOLOR=#000000[/COLOR] [COLOR=#000000]{[/COLOR]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;traceCOLOR=#000000[/COLOR];  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
yV = yV + Gravity;  
xV = xV \* Friction;  
x += xV;  
y += yV;  
[COLOR=#808080]_//Set rotation by calculating angle to where the object will be located next frame_[/COLOR]  
rotation = rotate2Point[COLOR=#000000]([/COLOR][COLOR=#0000FF]Math[/COLOR].[COLOR=#0000FF]atan2[/COLOR][COLOR=#000000]([/COLOR]COLOR=#000000[/COLOR]-y, COLOR=#000000[/COLOR]-x[COLOR=#000000])[/COLOR][COLOR=#000000])[/COLOR]+[COLOR=#000080]180[/COLOR];

```
        [COLOR=#808080]*//ADD TRAIL HERE*[/COLOR]

    [COLOR=#000000]}[/COLOR]
    [COLOR=#0000FF]private[/COLOR] [COLOR=#000000] **function** [/COLOR] remove[COLOR=#000000]([/COLOR][COLOR=#000000])[/COLOR] [COLOR=#000000]{[/COLOR]
        projectileControl.[COLOR=#000080]removeEventListener[/COLOR][COLOR=#000000]([/COLOR]TimerEvent.[COLOR=#000080]TIMER[/COLOR], projectileFunction[COLOR=#000000])[/COLOR];
    [COLOR=#000000]}[/COLOR]
    [COLOR=#0000FF]private[/COLOR] [COLOR=#000000] **function** [/COLOR] rotate2Point[COLOR=#000000]([/COLOR]r[COLOR=#000000])[/COLOR] [COLOR=#000000]{[/COLOR]
        [COLOR=#0000FF]return[/COLOR] r * [COLOR=#000080]180[/COLOR] / [COLOR=#0000FF]Math[/COLOR].[COLOR=#0000FF]PI[/COLOR];
    [COLOR=#000000]}[/COLOR]
[COLOR=#000000]}[/COLOR]

```

[COLOR=#000000]}[/COLOR]  
[/LEFT]

```
ActionScript Code:
[LEFT][COLOR=#000000] **function** [/COLOR] fireProjectile[COLOR=#000000]([/COLOR][COLOR=#000000])[/COLOR] [COLOR=#000000]{[/COLOR]
[COLOR=#000000] **var** [/COLOR] myBuggy = gameLevel.[COLOR=#000080]getChildByName[/COLOR][COLOR=#000000]([/COLOR]Players[COLOR=#000000][[/COLOR]myId[COLOR=#000000]][/COLOR].[COLOR=#000080]childName[/COLOR][COLOR=#000000])[/COLOR];
[COLOR=#000000] **var** [/COLOR] Projectile:Cannonball = [COLOR=#000000] **new** [/COLOR] Cannonball[COLOR=#000000]([/COLOR][COLOR=#000080]100[/COLOR][COLOR=#000000])[/COLOR];
Projectile.[COLOR=#000080]x[/COLOR] = myBuggy.[COLOR=#000080]Turret[/COLOR].[COLOR=#000080]myPoint[/COLOR].[COLOR=#000080]gPoint[/COLOR].[COLOR=#000080]x[/COLOR] - mapScrollX;
Projectile.[COLOR=#000080]y[/COLOR] = myBuggy.[COLOR=#000080]Turret[/COLOR].[COLOR=#000080]myPoint[/COLOR].[COLOR=#000080]gPoint[/COLOR].[COLOR=#000080]y[/COLOR];
Projectile.[COLOR=#000080]rotation[/COLOR] = myBuggy.[COLOR=#000080]rotation[/COLOR] + myBuggy.[COLOR=#000080]Turret[/COLOR].[COLOR=#000080]rotation[/COLOR];
gameLevel.[COLOR=#000080]addChild[/COLOR][COLOR=#000000]([/COLOR]Projectile[COLOR=#000000])[/COLOR];

```

[COLOR=#000000]}[/COLOR]  
[/LEFT]

[/quote]

---

<div class="post-metadata">

**Author:** ![TheCanadian](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@TheCanadian](https://forum.kirupa.com/u/TheCanadian)\
**Post date:** [July 4, 2008, 12:11am UTC](https://forum.kirupa.com/t/class-variables-not-being-set/265095/4 "2008-07-04T00:11:50Z")

</div>

You’re welcome :hoser:
