I have a class Game
in which I have a variable pointsCount
to count the points, which starts with 0 in constructor.
//my Game class
package
{
public var pointsCount:Number;
public static const status_begin:int = 1;
public function Game {}
{
pointsCount = 0;
}
Then I have a function points()
to handle the points:
public function points():void
{
if(ball.randomIndex == 0)
{
pointsCount++;
}
if(ball.randomIndex == 1)
{
pointsCount--;
}
....
}
Then I have a function to change level:
public function changeLevel():void
{
if(pointsCount == 4)
{
statusGame(4);
}
else if(pointsCount == 8)
{
statusGame(5);
}
And I have my statusGame
function to "control" the game status:
public function statusGame(status:int):void
{
switch(status)
{
case 1:
this.startGame();
break;
case 2:
ballBroken = false;
ball = newBall(this,velX,velY);
break;
case 3:
ballBroken = true;
points();
break;
case 4: trace("Level 2") ;
velX = 15;
velY = -2;
break;
case 5:trace("Level 3") ;
velX = 18;
velY = -2;
break;
My function to start the game which is called in status 1 (case 1):
public function startGame():void
{
ball = new Ball(this,10,-4);
}
I also have a .fla document where I have my game scenery and actions frame with this code:
import Game;
game.gameStatus(Game.status_begin); //to start the game
**if(Game.pointsCount)
{
trace("points 10");
**game.removeEventListener(Event.ENTER_FRAME, _enterFrame);**
gotoAndStop(3);
}**
I import my class Game
then I create an object to access my function gameStatus
to start the game.
Now when the points are equal to 10 I want to use gotoAndStop(3)
method to go to the frame 3.
However, my pointsCount
variable is always 0, because in my Game
constructor I declared pointsCount = 0
. I want to access pointsCount
when it is equal to 10; how can I do that?
I think the problem is because I handle pointsCount
inside my function changeLevel()
. How can I access this variable in my .fla document? I'm trying to solve this but nothing works..
You've got some basic syntax errors in your code, but I assume they're just in the question, and your actual code is compiling fine.
The problem seems to be that you're only checking for pointsCount to change one time. There are a couple of ways you can fix that, either by doing your check in the main timeline every frame, or by handling custom events that are fired off from your game class itself.
The first method is by far the easiest, if not quite as elegant as the latter. Try leaving your internal code the same (fixing any syntax errors that might exist)
public var pointsCount:Number;
public static const status_begin:int = 1;
//Constructor
public function Game()
{
pointsCount = 0;
}
//points incrementing and decrementing
public function points():void
{
if(ball.randomIndex == 0)
{
pointsCount++;
}
if(ball.randomIndex == 1)
{
pointsCount--;
}
//Other code.
}
//Changing the level.
public function changeLevel():void
{
if(pointsCount == 4)
{
statusGame(4);
}
else if(pointsCount == 8)
{
statusGame(5);
}
}
Then rewrite your check on the main timeline to repeat every frame.
import Game;
import flash.events.*;
//make a new game.
var game:Game = new Game();
//Add an event listener for each frame.
this.addEventListener(Event.ENTER_FRAME, checkScore);
//Start the game.
game.statusGame(Game.status_begin);
//Check when points equal 10.
function checkScore(e:Event)
{
if(game.pointsCount == 10)
{
trace("points 10");
gotoAndStop(3);
}
}
Depending on what you want to happen with your game, you may find it necessary to stop checking the points value after you move to frame 3. You can remove an event listener with this.removeEventListener(Event.ENTER_FRAME, checkScore);