Copy link to clipboard
Copied
Hi
I need to know how to stop a function from happening until I get to a certain frame
I'm using class files
Thanks
Copy link to clipboard
Copied
from any class that has access to any display object you can check any movieclip's currentFrame property and determine whether to execute your function.
Copy link to clipboard
Copied
What would I need to put?
Thanks
Copy link to clipboard
Copied
what movieclip do you want to check? are you checking from a class that has a display object reference?
Copy link to clipboard
Copied
here is the function in my game class:
what i want to do is stop it from happening until i get to frame 2
what do i need to do?
function Game(){
Key.initialize(stage);
ship = new Ship();
ship.x = 300;
ship.y = 150;
addChild(ship);
enemyShipTimer = new Timer(1500);
enemyShipTimer.addEventListener("timer", sendEnemy);
enemyShipTimer.start();
RocketShipTimer = new Timer(3000);
RocketShipTimer.addEventListener("timer", sendEnemyRocket);
RocketShipTimer.start();
Bombtimer = new Timer(7000);
Bombtimer.addEventListener("timer", sendBomb);
Bombtimer.start();
powerUpTimer = new Timer(10000);
powerUpTimer.addEventListener("timer", sendPowerUp);
powerUpTimer.start();
miniBossTimer = new Timer(20000);
miniBossTimer.addEventListener("timer", sendMiniBoss);
miniBossTimer.start();
bossCountdown = 3;
scoreText = new TextField();
scoreText.x = 290;
scoreText.text = String(0);
addChild(scoreText);
var scoreFormat = new TextFormat("Comic Sans MS", 20, 0x000000);
scoreText.defaultTextFormat = scoreFormat;
healthMeter = new HealthMeter();
healthMeter.x = 10;
healthMeter.y = 10;
addChild(healthMeter);
enemyHealthMeter = new EnemyHealthMeter();
enemyHealthMeter.x = 530;
enemyHealthMeter.y = 10;
addChild(enemyHealthMeter);
enemyHealthMeter.visible = false;
resetScore();
gameOverMenu = new GameOverMenu();
gameOverMenu.x = 80;
gameOverMenu.y = 35;
addChild(gameOverMenu);
gameOverMenu.visible = false;
gameOverMenu.playAgainButton.addEventListener("mouseDown", newGame);
ship.shield.visible = false;
}
Copy link to clipboard
Copied
that looks like a similar post made recently where Game is not just a function, it's a class constructor.
if that's true, i've already told you how to delay execution of that code: change the name of your document class.
Copy link to clipboard
Copied
so change the name of the class?
then how to i make it start when i get to frame 2?
thanks
Copy link to clipboard
Copied
as explained in your other thread on this subject, click an empty part of the stage (or the back stage) and, in the properties panel, change the document class from Game to something else (eg, Main).
in Main, start your menu and shop display and, when the user is ready to start playing, instantiate a Game instance by using something like:
var game:Game=new Game();
addChild(game);
and in Game.as you'll need to use an Event.ADDED_TO_STAGE event to execute the code that's currently in your Game constructor.
Copy link to clipboard
Copied
could you do me an example of this?
i have done the first part fine
but i dont really get:
and you mean start playing by them pressing a button how would i do that?
var game:Game=new Game();
addChild(game);
and in Game.as you'll need to use an Event.ADDED_TO_STAGE event to execute the code that's currently in your Game constructor.
Copy link to clipboard
Copied
well, something must happen in your new document class that indicates the game should start. maybe a button is clicked. if so, in the button listener function use:
var game:Game=new Game();
addChild(game);
and your game class should be changed to:
package{
//import all the needed classes
public class Game extends MovieClip{
//variable declarations
public function Game(){
this.addEventListener(Event.ADDED_TO_STAGE,init);
}
function init(e:Event):void{
Key.initialize(stage);
ship = new Ship();
ship.x = 300;
ship.y = 150;
addChild(ship);
enemyShipTimer = new Timer(1500);
enemyShipTimer.addEventListener("timer", sendEnemy);
enemyShipTimer.start();
RocketShipTimer = new Timer(3000);
RocketShipTimer.addEventListener("timer", sendEnemyRocket);
RocketShipTimer.start();
Bombtimer = new Timer(7000);
Bombtimer.addEventListener("timer", sendBomb);
Bombtimer.start();
powerUpTimer = new Timer(10000);
powerUpTimer.addEventListener("timer", sendPowerUp);
powerUpTimer.start();
miniBossTimer = new Timer(20000);
miniBossTimer.addEventListener("timer", sendMiniBoss);
miniBossTimer.start();
bossCountdown = 3;
scoreText = new TextField();
scoreText.x = 290;
scoreText.text = String(0);
addChild(scoreText);
var scoreFormat = new TextFormat("Comic Sans MS", 20, 0x000000);
scoreText.defaultTextFormat = scoreFormat;
healthMeter = new HealthMeter();
healthMeter.x = 10;
healthMeter.y = 10;
addChild(healthMeter);
enemyHealthMeter = new EnemyHealthMeter();
enemyHealthMeter.x = 530;
enemyHealthMeter.y = 10;
addChild(enemyHealthMeter);
enemyHealthMeter.visible = false;
resetScore();
gameOverMenu = new GameOverMenu();
gameOverMenu.x = 80;
gameOverMenu.y = 35;
addChild(gameOverMenu);
gameOverMenu.visible = false;
gameOverMenu.playAgainButton.addEventListener("mouseDown", newGame);
ship.shield.visible = false;
}
}
}
Copy link to clipboard
Copied
im getting it know thanks but there is one last thing i did all that and it works but i get lots of errors like this in the output file:
: Error #1009: Cannot access a property or method of a null object reference.
at Game/sendEnemy()
at flash.utils::Timer/_timerDispatch()
at flash.utils::Timer/tick()
or
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at EnemyShip/shoot()
at flash.utils::Timer/_timerDispatch()
at flash.utils::Timer/tick()
there are loads of these for all my enemies
there is also this for the explosion:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Explosion/enterFrame()
and
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Game/sendEnemyRocket()
at flash.utils::Timer/_timerDispatch()
at flash.utils::Timer/tick()
i think these are not inishiated when the function happens correct me.
any help
thanks
Copy link to clipboard
Copied
copy and paste your new Game class.
indicate which lines are 156 and 201.
also, show
where an EnemyShip is created and show line 92 in EnemyShip.as
where an Explosion instance is created and show line 18 in Explosion.as
Copy link to clipboard
Copied
here is the whole Game class with lines marked
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.utils.Timer;
import flash.text.TextField;
import flash.text.TextFormat;
public class Game extends MovieClip
{
static var ship:MovieClip;
static var enemyShipTimer:Timer;
static var scoreText:TextField;
static var score:Number;
static var healthMeter:HealthMeter;
static var enemyHealthMeter:EnemyHealthMeter;
static var gameOverMenu:GameOverMenu;
static var powerUpTimer:Timer;
static var miniBossTimer:Timer;
static var RocketShipTimer:Timer;
var bossCountdown:Number;
static var Bombtimer:Timer;
public function Game(){
this.addEventListener(Event.ADDED_TO_STAGE,init);
}
function init(e:Event):void{
Key.initialize(stage);
ship = new Ship();
ship.x = 300;
ship.y = 150;
addChild(ship);
enemyShipTimer = new Timer(1500);
enemyShipTimer.addEventListener("timer", sendEnemy);
enemyShipTimer.start();
RocketShipTimer = new Timer(3000);
RocketShipTimer.addEventListener("timer", sendEnemyRocket);
RocketShipTimer.start();
Bombtimer = new Timer(7000);
Bombtimer.addEventListener("timer", sendBomb);
Bombtimer.start();
powerUpTimer = new Timer(10000);
powerUpTimer.addEventListener("timer", sendPowerUp);
powerUpTimer.start();
miniBossTimer = new Timer(20000);
miniBossTimer.addEventListener("timer", sendMiniBoss);
miniBossTimer.start();
bossCountdown = 3;
scoreText = new TextField();
scoreText.x = 290;
scoreText.text = String(0);
addChild(scoreText);
var scoreFormat = new TextFormat("Comic Sans MS",20,0x000000);
scoreText.defaultTextFormat = scoreFormat;
healthMeter = new HealthMeter();
healthMeter.x = 10;
healthMeter.y = 10;
addChild(healthMeter);
enemyHealthMeter = new EnemyHealthMeter();
enemyHealthMeter.x = 530;
enemyHealthMeter.y = 10;
addChild(enemyHealthMeter);
enemyHealthMeter.visible = false;
resetScore();
gameOverMenu = new GameOverMenu();
gameOverMenu.x = 80;
gameOverMenu.y = 35;
addChild(gameOverMenu);
gameOverMenu.visible = false;
gameOverMenu.playAgainButton.addEventListener("mouseDown", newGame);
ship.shield.visible = false;
}
static function gameOver()
{
gameOverMenu.visible = true;
enemyShipTimer.stop();
miniBossTimer.stop();
RocketShipTimer.stop();
Bombtimer.stop();
for (var i in EnemyShip.list)
{
EnemyShip.list.kill();
}
for (var i in RocketShip.list)
{
RocketShip.list.kill();
}
for (var i in Bomb.list)
{
Bomb.list.kill();
}
}
function newGame(e:Event)
{
gameOverMenu.visible = false;
ship.visible = true;
ship.x = 300;
ship.y = 150;
ship.takeDamage(-ship.maxHealth);
ship.addEventListener("enterFrame", ship.move);
resetScore();
enemyShipTimer.start();
miniBossTimer.start();
RocketShipTimer.start();
Bombtimer.start();
}
function sendPowerUp(e:Event)
{
var powerUp = new PowerUp();
stage.addChild(powerUp);
}
function sendEnemy(e:Event)
{
var enemy = new EnemyShip();
here is line 156 stage.addChild(enemy);
}
function sendMiniBoss(e:Event)
{
miniBossTimer.stop();
if (bossCountdown == 0)
{
var boss = new Boss();
stage.addChild(boss);
bossCountdown = 3;
}
else
{
var miniBoss = new MiniBoss();
stage.addChild(miniBoss);
bossCountdown -= 1;
}
}
static function updateScore(points)
{
score += points;
scoreText.text = String(score);
}
static function resetScore()
{
score = 0;
scoreText.text = String(score);
}
function sendEnemyRocket(e:Event)
{
var enemy = new RocketShip();
here is line 201 stage.addChild(enemy);
}
function sendBomb(e:Event)
{
var enemy = new Bomb();
stage.addChild(enemy);
}
}
}
here is enemy ship class:
package{
import flash.display.MovieClip;
import flash.events.Event;
import flash.utils.Timer;
public class EnemyShip extends MovieClip{
var speed:Number;
static var list:Array = new Array();
var shootTimer:Timer;
var health:Number;
function EnemyShip (){
list.push(this);
this.x = 700;
this.y = Math.random()*200 + 50;
speed = Math.random()*5 + 5;
addEventListener("enterFrame", enterFrame);
var interval:Number = Math.random()*700 + 1000;
shootTimer = new Timer(interval);
shootTimer.addEventListener("timer", shoot);
shootTimer.start();
health = 1;
}
function takeDamage(d){
health -= d;
if(health <= 0){
Game.updateScore(50);
var pd = new PointDisplay();
pd.displayText.text = String(50);
pd.x = this.x;
pd.y = this.y;
stage.addChild(pd);
kill();
}
}
function enterFrame(e:Event){
this.x -= speed;
if(this.x < -100){
kill();
return;
}
if(this.hitTestObject(Game.ship)){
kill();
if(Game.ship.shield.visible == false){
Game.ship.takeDamage(20);
}
}
}
function kill(){
var explosion = new Explosion();
stage.addChild(explosion);
explosion.x = this.x;
explosion.y = this.y;
shootTimer.stop();
shootTimer.removeEventListener("timer", shoot);
removeEventListener("enterFrame", enterFrame);
stage.removeChild(this);
for(var i in list){
if(list == this){
delete list;
}
}
}
function shoot(e:Event){
var b = new EnemyBullet();
b.x = this.x - 50;
b.y = this.y;
LINE 92 HERE stage.addChild(b);
}
}
}
and here is the explosion:
package{
import flash.display.MovieClip;
import flash.events.Event;
public class Explosion extends MovieClip{
function Explosion(){
addEventListener("enterFrame", enterFrame);
}
function enterFrame(e:Event){
if(this.currentFrame == this.totalFrames){
removeEventListener("enterFrame", enterFrame);
Line 18 stage.removeChild(this);
}
}
}
}
hope this is ok
Copy link to clipboard
Copied
also here is a list of all the errors i get in the output file some of them are the same and are repeated
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Game/sendEnemy()
at flash.utils::Timer/_timerDispatch()
at flash.utils::Timer/tick()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at EnemyShip/shoot()
at flash.utils::Timer/_timerDispatch()
at flash.utils::Timer/tick()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Game/sendEnemy()
at flash.utils::Timer/_timerDispatch()
at flash.utils::Timer/tick()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Game/sendEnemyRocket()
at flash.utils::Timer/_timerDispatch()
at flash.utils::Timer/tick()
Copy link to clipboard
Copied
use the following trace statements to see which is null just above those two problematic lines in Game.as:
trace(enemy)
trace(stage)
Copy link to clipboard
Copied
so i put trace in where the 2 problems are in game.as
function sendEnemyRocket(e:Event)
{
trace(enemy)
trace(stage)
var enemy = new RocketShip();
stage.addChild(enemy);
}
if thats right what am i looking for
Copy link to clipboard
Copied
would it be eaiser if i put a dropbox link to my code and you can see whats wrong?
Copy link to clipboard
Copied
no, use:
function sendEnemy(e:Event)
{
var enemy = new EnemyShip();
trace(enemy)
trace(stage)
stage.addChild(enemy);
}
and
function sendEnemyRocket(e:Event)
{
var enemy = new RocketShip();
trace(enemy)
trace(stage)
stage.addChild(enemy);
}
start your game and report the trace outputs (not the error messages)
Copy link to clipboard
Copied
this all the stuff i get in the output:
[object EnemyShip]
null
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Game/sendEnemy()
at flash.utils::Timer/_timerDispatch()
at flash.utils::Timer/tick()
[object EnemyShip]
[object Stage]
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at EnemyShip/shoot()
at flash.utils::Timer/_timerDispatch()
at flash.utils::Timer/tick()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Ship/move()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Ship/move()
[object EnemyShip]
null
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Game/sendEnemy()
at flash.utils::Timer/_timerDispatch()
at flash.utils::Timer/tick()
[object RocketShip]
null
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Game/sendEnemyRocket()
at flash.utils::Timer/_timerDispatch()
at flash.utils::Timer/tick()
[object EnemyShip]
[object Stage]
[object RocketShip]
[object Stage]
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at EnemyBullet/enterFrame()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Bullet/enterFrame()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at EnemyShip/shoot()
at flash.utils::Timer/_timerDispatch()
at flash.utils::Timer/tick()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at EnemyBullet/enterFrame()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Bullet/enterFrame()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at EnemyShip/kill()
.
.
.
at EnemyShip/enterFrame()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Explosion/enterFrame()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at EnemyShip/kill()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Explosion/enterFrame()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Explosion/enterFrame()
[object EnemyShip]
null
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Game/sendEnemy()
at flash.utils::Timer/_timerDispatch()
at flash.utils::Timer/tick()
[object EnemyShip]
[object Stage]
hope its ok
Copy link to clipboard
Copied
replace your use of "stage" with _stage and add the following bolded lines:
package{
//import all the needed classes
public class Game extends MovieClip{
//variable declarations
private var _stage:Stage;
public function Game(){
this.addEventListener(Event.ADDED_TO_STAGE,init);
}
function init(e:Event):void{
_stage=stage;
Key.initialize(stage);
ship = new Ship();
ship.x = 300;
ship.y = 150;
addChild(ship);
enemyShipTimer = new Timer(1500);
enemyShipTimer.addEventListener("timer", sendEnemy);
enemyShipTimer.start();
RocketShipTimer = new Timer(3000);
RocketShipTimer.addEventListener("timer", sendEnemyRocket);
RocketShipTimer.start();
Bombtimer = new Timer(7000);
Bombtimer.addEventListener("timer", sendBomb);
Bombtimer.start();
powerUpTimer = new Timer(10000);
powerUpTimer.addEventListener("timer", sendPowerUp);
powerUpTimer.start();
miniBossTimer = new Timer(20000);
miniBossTimer.addEventListener("timer", sendMiniBoss);
miniBossTimer.start();
bossCountdown = 3;
scoreText = new TextField();
scoreText.x = 290;
scoreText.text = String(0);
addChild(scoreText);
var scoreFormat = new TextFormat("Comic Sans MS", 20, 0x000000);
scoreText.defaultTextFormat = scoreFormat;
healthMeter = new HealthMeter();
healthMeter.x = 10;
healthMeter.y = 10;
addChild(healthMeter);
enemyHealthMeter = new EnemyHealthMeter();
enemyHealthMeter.x = 530;
enemyHealthMeter.y = 10;
addChild(enemyHealthMeter);
enemyHealthMeter.visible = false;
resetScore();
gameOverMenu = new GameOverMenu();
gameOverMenu.x = 80;
gameOverMenu.y = 35;
addChild(gameOverMenu);
gameOverMenu.visible = false;
gameOverMenu.playAgainButton.addEventListener("mouseDown", newGame);
ship.shield.visible = false;}
}
}
Copy link to clipboard
Copied
hi
i get this error when i change all stage to _stage and put in that code you said
C:\Users\James\Desktop\source_files\Game.as, Line 27 1046: Type was not found or was not a compile-time constant: Stage.
Copy link to clipboard
Copied
import the needed classes:
import flash.display.Stage;
Copy link to clipboard
Copied
now my hero and enemies dont appear and i get this in the output
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Key$/initialize()
at Game/init()
at flash.display::DisplayObjectContainer/addChild()
at Main/onClick()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Key$/initialize()
at Game/init()
at flash.display::DisplayObjectContainer/addChild()
at Main/onClick()
Copy link to clipboard
Copied
you probably need to use another addedtostage event.
what's line 22 in Key? does it reference the stage?
Copy link to clipboard
Copied
here is the code for key what do you meen about addtostage event and where shall i puy it
/*
This class get initialized in the Constructor of the Game class.
This class manages basic key presses, user input.
You can just use this class by calling: Key.isDown(Keyboard.LEFT) from other classes like in AS2.0.
*/
package
{
import flash.display.Stage;
import flash.events.Event;
import flash.events.KeyboardEvent;
public class Key {
private static var initialized:Boolean = false;
private static var keysDown:Object = new Object(); // stores key codes of all keys pressed
public static function initialize(stage:Stage) {
if (!initialized) {
// assign listeners for key presses and deactivation of the player
LINE 22 stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
stage.addEventListener(Event.DEACTIVATE, clearKeys);
// mark initialization as true so redundant
// calls do not reassign the event handlers
initialized = true;
}
}
public static function isDown(keyCode:uint):Boolean
{
return Boolean(keyCode in keysDown);
}
private static function keyPressed(event:KeyboardEvent):void {
keysDown[event.keyCode] = true;
}
private static function keyReleased(event:KeyboardEvent):void {
if (event.keyCode in keysDown) {
delete keysDown[event.keyCode];
}
}
private static function clearKeys(event:Event):void {
// clear all keys in keysDown since the player cannot detect keys being pressed or released when not focused
keysDown = new Object();
}
}
}
Find more inspiration, events, and resources on the new Adobe Community
Explore Now