質問

I am trying to make a game similar to pong, and I am putting the code into each class of my movieclips instead of into the timeline. I have a variable ballspeedx in my class ball. What ballspeedx does is change the speed of which the object moves horizontally. I am trying to call that variable in that class from my main class, named main. Not only do I want to be able to call it, I want to be able to change it and then return the changed value. Can someone explain how to do that?

役に立ちましたか?

解決

Make your variable "ballspeedx" a public variable in "ball" class. When you create its object in main class, you can access its public variables. You can also change this. You can add getter and setter functions in "ball" class to change/access the variable value if its private.

Ball.as:

public var ballspeedx:int = 10;

Main.as:

var ball:Ball = new Ball();
trace(ball.ballspeedx);
ball.ballspeedx = 20;
trace(ball.ballspeedx);

他のヒント

You can make ballspeedx a public member of your ball class.

public var ballspeedx:Number;

OR, you can create a getter and setter for the private property:

private var _ballspeedx:Number;

public function get ballspeedx():Number { return _ballspeedx:Number; }
public function set ballspeedx($value:Number):void { _ballspeedx = $value; }
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top