我刚开始编写游戏编程,当我运行代码时,我说
1064:未找到类型或不是编译时间常数:事件
1064:未找到类型或不是编译时间常数:鼠标事件
以下是代码:
package{
public class Script_1 {
public static const STATE_INIT:int = 10
public static const STATE_PLAY:int = 20
public static const STATE_GAME_OVER:int = 30
public var gameState:int = 0
public function gameLoop(e:Event):void{
switch(gameState) {
case STATE_INIT:
initGame();
break;
case STATE_PLAY:
playGame();
break;
case STATE_GAME_OVER:
gameOver();
break;
}
}
public function Game(){
addEventListener(Event.ENTER_FRAME, gameLoop);
gameState = STATE_INIT;
}
stage.addEventListener(MouseEvent.CLICK, onMouseClickEvent);
public function initGame():void{
stage.addEventListener(MouseEvent.CLICK, onMouseClickEvent);
clicks = 0
gameState = STATE_PLAY;
}
public function playGame(){
if (clicks >= 10){
gameState = STATE_GAME_OVER;
}
}
public function onMouseClickEvent(e:MouseEvent):void{
clicks++;
trace("mouse click number:" + clicks);
}
public function gameOver():void{
stage.removeEventListener(MouseEvent.CLICK, onMouseClickEvent);
gameState = STATE_INIT;
trace("game over");
}
}
}
这是一个名为Script_1.as
的文件答案 0 :(得分:1)
您需要使用import
语句导入这些类。缺少每个类且属于类定义的类需要此语句:
package
{
// Imports.
import flash.events.Event;
import flash.events.MouseEvent;
public class Script_1
{
// ..
}
}
另外,我注意到了一些错误的事情:
addEventListener()
,但Script_1
未延伸EventDispatcher
或至少实施IEventDispatcher
。根据您想要收听的事件,Sprite
似乎最合适。Game
,或者您的构造函数Game()
应该是Script_1()
。