Forums → Programming Forum → Another problem
28 | 24630 |
I get the error
CountdownDisplayMain.as, Line 16 1119: Access of possibly undefined property CountdownClockDisplay through a reference with static type Class.
and also the same on line 27
package {
import flash.display.MovieClip;
import flash.events.TimerEvent;
import flash.utils.Timer;
public class CountdownDisplayMain extends MovieClip {
var time:Number;
var timeTimer:Timer;
public function CountdownDisplayMain()
{
time + 60;
CountdownDisplayMain.CountdownClockDisplay.text = time.toString();
timeTimer = new Timer(1000);
timeTimer.addEventListener(TimerEvent.TIMER,countdown);
timeTimer.start();
}
function countdown(Event:TimerEvent)
{
time -= 1;
CountdownDisplayMain.CountdownClockDisplay.text = time.toString();
}
}
}
In my document class i have the following
var countdownDisplayMain:CountdownDisplayMain = new CountdownDisplayMain();
countdownDisplayMain.x = 50;
countdownDisplayMain.y = 350;
addChild(countdownDisplayMain);
- 28 Replies
note: if you are using an event class the dispatcher would look like this
dispatchEvent(new GameEvent(GameEvent.GAMEOVER));
Got no errors but it isn't showing the darkGameScreen screen when the time has reached 0.
package {
import flash.events.*;
public class GameEvent extends Event {
public static const GAMEOVER:String = "gameover";
public function GameEvent(type:String)
{
super(type);
}
}
}
package {
import flash.display.MovieClip;
import flash.events.*;
public class Document extends MovieClip{
public var game:GameClass;
var darkGameScreenarkBackScreen;
public function Document()
{
game = new GameClass(stage);
game.addEventListener(GameEvent.GAMEOVER, onGameFinish);
addChild(game);
}
public function onGameFinish(GameEvent)
{
trace("OnFinishFunction working"
darkGameScreen = new DarkBackScreen();
darkGameScreen.x = 0;
darkGameScreen.y = 0;
addChild(darkGameScreen);
game = null;
}
}
}
package {
import flash.display.MovieClip;
import flash.events.TimerEvent;
import flash.utils.Timer;
import flash.events.*;
public class CountdownDisplayMain extends MovieClip {
var time:Number;
var timeTimer:Timer;
public function CountdownDisplayMain()
{
time = 60;
CountdownClockDisplay.text = time.toString();
timeTimer = new Timer(1000);
timeTimer.addEventListener(TimerEvent.TIMER,countdown);
timeTimer.start();
}
function countdown(Event:TimerEvent)
{
time -= 1;
CountdownClockDisplay.text = time.toString();
if(time == 0)
{
timeTimer.stop();
dispatchEvent(new GameEvent(GameEvent.GAMEOVER));
}
}
}
}
okie I have a theory you could test and I'll explain it, but bear with me this is untested
game.addEventListener(GameEvent.GAMEOVER, onGameFinish);
it is not the game class that is dispatching the event, but how can you add a listener to a class that wasn't declared as a variable... well you cant, so what you need to do is bubble the dispatching event from child to parent in this case the parent is the GameClass and the child is CountdownDisplayMain
dispatchEvent(new GameEvent(GameEvent.GAMEOVER, true));
error: C:\\Users\\Alex The Best\\Documents\\My Games\\Platformer\\Classes\\CountdownDisplayMain.as, Line 33 1137: Incorrect number of arguments. Expected no more than 1.
I think the dispatch events are working just i put a trace statement into the
if (time == 0); thingy
and it didn't show the trace statement but the timeTimer does stop at 0 so that should be working. Its just the darkGameScreen which isn't showing.
alrighty fail on my theory, try doing this
function countdown(Event:TimerEvent)
{
if(time == 0)
{
timeTimer.stop();
timeTimer.removeEventListener(TimerEvent.TIMER,countdown);
trace("dispatching event"
dispatchEvent(new GameEvent(GameEvent.GAMEOVER));
}
else
{
time -= 1;
CountdownClockDisplay.text = time.toString();
}
}
It has stayed the same no errors or warning just not showing the trace statement dispatching event it shows 2 other trace statements in the avatar class and gameclass but not this one.
I tested this exact code and it worked for me maybe if you put it in the CountdownDisplayMain function instead of a separate function
var time:Number = 60;
var timeTimer:Timer;
timeTimer = new Timer(1000);
timeTimer.addEventListener(TimerEvent.TIMER,countdown);
timeTimer.start();
function countdown(Event:TimerEvent)
{
if(time == 0)
{
timeTimer.stop();
timeTimer.removeEventListener(TimerEvent.TIMER,countdown);
trace("dispatching event"
//dispatchEvent(new GameEvent(GameEvent.GAMEOVER));
}
else
{
time -= 1;
trace(time);
}
}
I put it in the CountdownDisplayMain function and it plays the dispatching event trace statement but doesn't show the dark screen when it reaches 0. No errors either.
package {
import flash.display.MovieClip;
import flash.events.TimerEvent;
import flash.utils.Timer;
import flash.events.*;
public class CountdownDisplayMain extends MovieClip {
var time:Number;
var timeTimer:Timer;
public function CountdownDisplayMain()
{
time = 60;
CountdownClockDisplay.text = time.toString();
timeTimer = new Timer(1000);
timeTimer.addEventListener(TimerEvent.TIMER,countdown);
timeTimer.start();
function countdown(Event:TimerEvent)
{
time -= 1;
CountdownClockDisplay.text = time.toString();
if(time == 0)
{
timeTimer.stop();
timeTimer.removeEventListener(TimerEvent.TIMER, countdown);
trace("dispatchingEvent"
dispatchEvent(new GameEvent(GameEvent.GAMEOVER));
}
else
{
time == 0;
CountdownClockDisplay.text = time.toString();
}
}
}
}
package {
import flash.display.MovieClip;
import flash.events.*;
public class Document extends MovieClip{
public var game:GameClass;
var darkGameScreenarkBackScreen;
public function Document()
{
game = new GameClass(stage);
game.addEventListener(GameEvent.GAMEOVER, onGameFinish);
addChild(game);
}
public function onGameFinish(GameEvent)
{
trace("OnFinishFunction working"
darkGameScreen = new DarkBackScreen();
darkGameScreen.x = 0;
darkGameScreen.y = 0;
addChild(darkGameScreen);
game = null;
}
}
}
package {
import flash.events.*;
public class GameEvent extends Event {
public static const GAMEOVER:String = "gameover";
public function GameEvent(type:String)
{
super(type);
}
}
}
if you are getting the trace then to my understanding it should be working, you could try the other method by commenting out this line and adding in your document class
game = new GameClass(stage);
//game.addEventListener(GameEvent.GAMEOVER, onGameFinish);
game.addEventListener("ENDGAME", dispatchTest);
addChild(game);
add this function like you did the onGameFinish
public function dispatchTest(event):void
{
trace("it works"
}
and your CountdownDisplayMain class
if(time > 0)
{
time -= 1;
CountdownClockDisplay.text = time.toString();
}
else
{
timeTimer.stop();
timeTimer.removeEventListener(TimerEvent.TIMER, countdown);
trace("dispatchingEvent"
//dispatchEvent(new GameEvent(GameEvent.GAMEOVER));
dispatchEvent(new Event("ENDGAME", true));
}
This is so aggravating to the point i wanna give up and start again
dispatchEvent( new Event("ENDGAME", true));
C:\\Users\\Alex The Best\\Documents\\My Games\\Platformer\\Classes\\CountdownDisplayMain.as, Line 35 1180: Call to a possibly undefined method Event.
and then I changed new Event ("ENDGAME"
to new GameEvent("ENDGAME"
and then it said incorrect number of arguments
Flash thinks that you are using the passing argument as an Event. In your CountdownDisplayMain class
change this
function countdown(Event:TimerEvent)
to this
function countdown(event):void
You know like I have done game = null
what is that supposed to do because i got it from a tutorial and it said it should remove game from sight I think and I just wanted want you would do instead.
Anyway game = null in my code doesn't do anything.
public function Document():void
{
game = new GameClass(stage);
game.addEventListener("GAMEOVER", onGameFinish);
addChild(game);
}
public function onGameFinish(event):void
{
game.removeEventListener("GAMEOVER", onGameFinish);
removeChild(game);
game = null;
darkGameScreen = new DarkBackScreen();
darkGameScreen.x = 0;
darkGameScreen.y = 0;
addChild(darkGameScreen);
}
You must be logged in to post a reply!