0

Okay, so first, I have searched for this everywhere but it seems like every answer is either overcomplicated or simply does not work, and I know for sure there should be a more simple way of achieving what I need.

So, until today, I have always coded from within the timeline. But now I realise why I should code in separate class files. However, I still want to include snippets of code in the timeline for simplicity's sake.

So in my Ship class I have this line of code:

    public var speed:int = 2 + Math.ceil(Math.random() * 4)

And in my timeline I have the code:

import Ship;
trace(Ship.speed)

I can't get the trace to display the speed. The class file executes perfectly on its own but when I try to access its speed variable (as above in the timeline), I get this:

Scene 1, Layer 'Actions', Frame 1, Line 2 1119: Access of possibly undefined property speed through a reference with static type Class.

So a simple question, and apologies for it, but can anyone give me a simple way to trace the speed from the Ship.as class file?

Thanks in advance!

hazdog
  • 121
  • 1
  • 3
  • 20

2 Answers2

2

You need to create a Ship instance, like this:

import Ship;
var ship:Ship = new Ship();
trace(ship.speed);

OR

You can declare speed as a static variable to access it without the need of an instance (but I think here it makes less sense):

public static var speed:int = 2;//or whatever

To learn more about static variables and methods in AS3, check this response: Actionscript 3: Can someone explain to me the concept of static variables and methods?

Community
  • 1
  • 1
danii
  • 5,553
  • 2
  • 21
  • 23
  • thanks for the quick edit mate. the static variable thing worked. i admit i haven't read your link and probably won't do until i finish with this project. i had some type error when i used your first code, but now it's all good. thanks for the speedy reply :) – hazdog Oct 24 '11 at 10:13
  • That seems to be an error inside your Ship class, in the code for your constructor (the Ship() function). Make sure all the objects you are using are initialized. Show me some code if you need help, but my advice is try to keep things simple: comment off everything but the speed var, and make sure you are able to access it from your timeline. Afterwards, uncomment the lines in your constructor one by one until you find what's giving you trouble. – danii Oct 24 '11 at 10:17
  • should i be initiating my variables inside my constructor? i just put them inside the ship class (public class ship extends sprite etc.) but before my constructor class, underneath their own little heading. – hazdog Oct 24 '11 at 10:24
  • That sounds ok. I would recommend you to try the non-static definition. If you start getting 1009 errors again, you know where to ask ;) – danii Oct 24 '11 at 10:39
0

inside your Ship class:

public function get speed():int{
    return 2 + Math.ceil(Math.random() * 4);
}

and on the timeline:

import Ship;
var ship:Ship = new Ship();
trace(ship.speed);
www0z0k
  • 4,444
  • 3
  • 27
  • 32