Forums

ForumsProgramming Forum

Another problem

Posted Dec 5, '13 at 2:49pm

alex57834

alex57834

89 posts

Trying to use dispatch events now

C:\\Users\\Alex The Best\\Documents\\My Games\\Platformer\\Classes\\CountdownDisplayMain.as, Line 32 1137: Incorrect number of arguments. Expected no more than 0.

line 32 is the dispatch event line



Document class

package {

import flash.display.MovieClip;

public class Document extends MovieClip{

public var game:GameClass;

public function Document()
{
game = new GameClass(stage);
game.addEventListener(GameEvent.FINISH, onGameFinish);
addChild(game);
}

public function onGameFinish(gameEvent:GameEvent)
{
var darkBackScreenarkBackScreen = new DarkGameScreen();
darkBackScreen.x = 0;
darkBackScreen.y = 0;
addChild(darkBackScreen);

game = null;
}

}

}



function countdown(Event:TimerEvent)
{
time -= 1;
CountdownClockDisplay.text = time.toString();

if(time == 0)
{
timeTimer.stop();
dispatchEvent(new GameEvent(GameEvent.FINISH));
}
}

}

}

 

Posted Dec 5, '13 at 3:24pm

weirdlike

weirdlike

1,065 posts

Moderator

it kind of looks like you're reference a dispatching class, although I personally don't like using a class for dispatched event's it would probably look something like this

package
{
import flash.events.*;
public class GameEvent extends Event
{
public static const STARTGAME:String = "start";
public static const RESTARTGAME:String = "restart";
public static const FINISH:String = "finish";

public function GameEvent(type:String)
{
super(type);
}
}
}

----------------------------------------------------------------
this is how I like to do it

in your document class the listener would look like this

game.addEventListener("dispatch", onGameFinish);

then the dispatcher in your game class will look like this

dispatchEvent(new Event("dispatch", true));

the text that is quoted can be changed to whatever ex. "GAMEOVER" but the listener and the dispatch must match

 

Posted Dec 5, '13 at 3:32pm

weirdlike

weirdlike

1,065 posts

Moderator

Oh yeah and if you use a dispatching class change this line

public function onGameFinish(gameEvent:GameEvent)

to this

public function onGameFinish(GameEvent):void

if you use my method then it would look like this

public function onGameFinish(event):void

 

Posted Dec 5, '13 at 4:31pm

alex57834

alex57834

89 posts

Got another error now

C:\\Users\\Alex The Best\\Documents\\My Games\\Platformer\\Classes\\CountdownDisplayMain.as, Line 33 1180: Call to a possibly undefined method Event.

 

Posted Dec 5, '13 at 4:32pm

alex57834

alex57834

89 posts

package {

import flash.events.*;

public class GameEvent extends Event {

public static const GAMEOVER:String = "gameover";

public function GameEvent(type:String)
{
super(type);
}

}

}






function countdown(Event:TimerEvent)
{
time -= 1;
CountdownClockDisplay.text = time.toString();

if(time == 0)
{
timeTimer.stop();
dispatchEvent(new Event("GAMEOVER", true)); = line33
}
}

}

}

 

Posted Dec 5, '13 at 5:25pm

weirdlike

weirdlike

1,065 posts

Moderator

I cant see the whole class, did you import events?

import flash.events.*;

 

Posted Dec 5, '13 at 5:29pm

weirdlike

weirdlike

1,065 posts

Moderator

note: if you are using an event class the dispatcher would look like this

dispatchEvent(new GameEvent(GameEvent.GAMEOVER));

 

Posted Dec 7, '13 at 4:26am

alex57834

alex57834

89 posts

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));
}
}

}

}

 

Posted Dec 7, '13 at 11:06am

weirdlike

weirdlike

1,065 posts

Moderator

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));

 

Posted Dec 7, '13 at 1:40pm

alex57834

alex57834

89 posts

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.

 
Reply to Another problem

You must be logged in to post a reply!