Hi!
I’m quite new to flash, and have been learning AS3 via tutorials and snippets online. I found this shooting the star game, and I needed some help in creating it…
http://flashadvanced.com/creating-small-shooting-game-as3/
I have created the flash.fla file as described in that tutorial, but I took it one step further by adding a DocumenClass.as to it… I declared all the functions in it and the variables too… but somehow it’s not working…
Here’s the “ShootingStarFullClass.as” file that I made using the same functions and variables with some alterations…
package { import flash.display.MovieClip;
//importing tween classes
import fl.transitions.easing.*;
import fl.transitions.Tween;
import flash.utils.Timer;
import flash.events.*;
import flash.ui.Mouse;
//Call this class the SAME NAME as the file - VERY IMPORTANT
class ShootingStarFullClass extends MovieClip
{
var star:Star = new Star();
//creating the timer
var timer:Timer = new Timer(1500);
var clickTime:Timer = new Timer(1);
//we create variables for random X and Y positions
var randomX:Number;
var randomY:Number;
//variable for the alpha tween effect
var tween:Tween;
//we check if a star instance is already added to the stage
var starAdded:Boolean = false;
//we count the points
var points:int = 0;
var t:int = 0;
var gameOver:Boolean = false;
public function ShootingStarFullClass()
{
//nothing needs to go here for you today...
}
public function timerHandler(e:TimerEvent):void {
//first we need to remove the star from the stage if already added
if(starAdded){
removeChild(star);
}
//positioning the star on a random position
randomX = Math.random()*500;
randomY = Math.random()*300;
star.x = randomX;
star.y = randomY;
//adding the star to the stage
addChild(star);
//changing our boolean value to true
starAdded = true;
//adding a mouse click handler to the star
star.addEventListener(MouseEvent.CLICK, clickHandler);
//animating the star's appearance
tween = new Tween(star, "alpha", Strong.easeOut, 0, 1, 3, true);
if(t>5) {
gameOver = true;
removeChild(star);
starAdded = false;
}
if(gameOver){
gotoAndPlay("gameover");
}
}
public function cursorMoveHandler(e:Event):void{
//sight position matches the mouse position
sight.x = mouseX;
sight.y = mouseY;
}
public function clickHandler(e:Event):void{
if(starAdded)
{
removeChild(star);
starAdded = false;
}
t++;
//when we click/shoot a star we increment the points
points ++;
//showing the result in the text field
points_txt.text = points.toString();
}
function frm10 (e:Event):void {
removeChild(sight);
Mouse.show();
timer.stop();
timer.removeEventListener(TimerEvent.TIMER, timerHandler);
stage.removeChild(star);
stage.removeEventListener(MouseEvent.MOUSE_MOVE, cursorMoveHandler);
stage.removeEventListener(MouseEvent.CLICK, clickHandler);
points_txt.text = points.toString();
if(starAdded){
removeChild(star);
starAdded = false;
removeChild(sight);
}
}
public function gameStart() {
//hiding the cursor
Mouse.hide();
timer.start();
}
}
}
The code for Frame 1 of the file is:
var Game:ShootingStarFullClass = new ShootingStarFullClass();
Game.gameStart();
//adding event handler on mouse move
stage.addEventListener(MouseEvent.MOUSE_MOVE, cursorMoveHandler);
//adding event handler to the timer
timer.addEventListener(TimerEvent.TIMER, timerHandler);
//clickTime.addEventListener(TimerEvent.TIMER, clicktimerHandler);
stop();
Frame 10 also holds some code as follows:
addEventListener(Event.ENTER_FRAME, frm10);stop();
When I play the swf output… it just loops between frame 1 and frame 10 jumping from 1 to 10 and not showing anything except the static objects.
Please can someone provide any help on how to get this code working?
I edited the file from the actual tutorial because:
*I wanted another scene/frame to show the user highscores etc.
*the variables weren’t being exported to frame 10, so I had to create the DocumentClass.as … (as suggested to someone by someone on another forum)