Actionscript 3 鼠标单击addEventListener不工作
这是我的密码:Actionscript 3 鼠标单击addEventListener不工作,actionscript-3,flash,actionscript,Actionscript 3,Flash,Actionscript,这是我的密码: package { import flash.display.MovieClip; import flash.events.*; import flash.geom.*; import flash.text.*; import flash.utils.Timer; import flash.events.TimerEvent; import flash.display.SimpleButton; impor
package
{
import flash.display.MovieClip;
import flash.events.*;
import flash.geom.*;
import flash.text.*;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.display.SimpleButton;
import Game.*;
public class GameController extends MovieClip
{
private var player:Jovia;
private var speed:Number;
private var gravity:Number;
private var randomChance:Number;
private var randomChance1:Number;
private var ungos:Array;
private var apples:Array;
private var score:Number;
public function GameController()
{
}
public function startMenu()
{
btnPlay.addEventListener(MouseEvent.CLICK, gotoGame);
btnHelp.addEventListener(MouseEvent.CLICK, gotoHowToPlay);
btnCredits.addEventListener(MouseEvent.CLICK, gotoCredits);
}
public function startGhost()
{
}
public function startHelp()
{
btnPlay.addEventListener(MouseEvent.CLICK, gotoGame);
}
public function startCredits()
{
btnPlay.addEventListener(MouseEvent.CLICK, gotoGame);
}
public function startGameOver()
{
gotoAndStop("gameOver");
txtScore.text = String(score);
btnPlay.addEventListener(MouseEvent.CLICK, gotoGame);
gravity = C.GRAVITY;
speed = C.PLAYER_SPEED;
}
private function gotoCredits(evt:MouseEvent)
{
btnPlay.addEventListener(MouseEvent.CLICK, gotoGame);
btnHelp.removeEventListener(MouseEvent.CLICK, gotoHowToPlay);
btnCredits.removeEventListener(MouseEvent.CLICK, gotoCredits);
gotoAndStop("credits");
}
private function gotoHowToPlay(evt:MouseEvent)
{
btnPlay.addEventListener(MouseEvent.CLICK, gotoGame);
btnHelp.removeEventListener(MouseEvent.CLICK, gotoHowToPlay);
btnCredits.removeEventListener(MouseEvent.CLICK, gotoCredits);
gotoAndStop("howtoplay");
}
private function gotoGame(evt:MouseEvent)
{
btnPlay.removeEventListener(MouseEvent.CLICK, gotoGame);
btnHelp.removeEventListener(MouseEvent.CLICK, gotoHowToPlay);
btnCredits.removeEventListener(MouseEvent.CLICK, gotoCredits);
gotoAndStop("game");
}
public function startGame()
{
speed = C.PLAYER_SPEED;
gravity = C.GRAVITY;
score = C.PLAYER_START_SCORE;
randomChance = C.APPLE_SPAWN_CHANCE;
randomChance1 = C.UNGO_SPAWN_CHANCE;
ungos = new Array();
apples = new Array();
player = new Jovia();
mcGameStage.addChild(player);
mcGameStage.addEventListener(Event.ENTER_FRAME,update);
}
private function update(evt:Event)
{
//******************
//Handle User Input
//******************
var currMouseX = mouseX;
var currMouseY = mouseY;
var moveX = 0;
var moveY = 0;
if (currMouseX > player.x)
{
moveX = 1;
}
else if (currMouseX < player.x)
{
moveX = -1;
}
if (currMouseY > player.y)
{
moveY = 1;
}
else if (currMouseY < player.y)
{
moveY = -1;
}
//******************
//Handle Game Logic
//******************
//Handle new Player Position
if (moveX > 0)
{
if (player.x + C.PLAYER_SPEED <= currMouseX)
player.x += C.PLAYER_SPEED;
}
else if (moveX < 0)
{
if (player.x - C.PLAYER_SPEED > currMouseX)
player.x -= C.PLAYER_SPEED;
}
if (moveY > 0)
{
if (player.y + C.PLAYER_SPEED <= currMouseY)
player.y += C.PLAYER_SPEED;
}
else if (moveY < 0)
{
if (player.y - C.PLAYER_SPEED > currMouseY)
player.y -= C.PLAYER_SPEED;
}
//Spawn Ungo
if (Math.random() < randomChance1)
{
var newUngo = new Ungo();
newUngo.x = Math.random() * C.UNGO_SPAWN_END_X + C.UNGO_SPAWN_START_X;
newUngo.y = C.UNGO_START_Y;
ungos.push(newUngo);
mcGameStage.addChildAt(newUngo,0);
}
//Move Ungo
for (var i = ungos.length-1; i >= 0; i--)
{
ungos[i].y += gravity;
if (ungos[i].y > C.UNGO_END_Y)
{
mcGameStage.removeChild(ungos[i]);
ungos.splice(i,1);
}
}
//Spawn new apples
if (Math.random() < randomChance)
{
var newApple = new Apple();
newApple.x = Math.random() * C.APPLE_SPAWN_END_X + C.APPLE_SPAWN_START_X;
newApple.y = C.APPLE_START_Y;
apples.push(newApple);
mcGameStage.addChildAt(newApple,0);
}
//Move Apples
for (var i = apples.length-1; i >= 0; i--)
{
apples[i].y += gravity;
if (apples[i].y > C.APPLE_END_Y)
{
mcGameStage.removeChild(apples[i]);
apples.splice(i,1);
}
}
//Check for collision
var playerPoint = new Point(player.x, player.y);
for (var i = apples.length-1; i >= 0; i--)
{
var applePoint:Point = new Point(apples[i].x, apples[i].y);
if (Point.distance(applePoint,playerPoint) < C.HIT_TOLERANCE)
{
//Register hit
score += C.SCORE_PER_APPLE;
mcGameStage.removeChild(apples[i]);
apples.splice(i,1);
}
}
//ungo collision
for (var i = ungos.length-1; i >= 0; i--)
{
var ungoPoint:Point = new Point(ungos[i].x, ungos[i].y);
if (Point.distance(ungoPoint,playerPoint) < C.HIT_TOLERANCE)
{
//Register hit
mcGameStage.removeChild(ungos[i]);
ungos.splice(i,1);
gotoAndStop("ghost");
}
}
//******************
//Handle Display
//******************
//Animate the player
if (moveX > 0)
{
if (player.currentLabel != C.JOVIA_RIGHT)
player.gotoAndPlay(C.JOVIA_RIGHT);
}
else if (moveX < 0)
{
if (player.currentLabel != C.JOVIA_LEFT)
player.gotoAndPlay(C.JOVIA_LEFT);
}
//Display new Score
txtScore.text = String(score);
}
}
}
游戏:
游戏结束:
stop();
startGameOver();
howtoplay:
stop();
startHelp();
学分:
stop();
startCredits();
鬼:
stop();
var snd_scream = new SoundScream();
snd_scream.play();
var myTimer:Timer = new Timer(2000, 1);
myTimer.start();
myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, function(e:TimerEvent){
startGameOver();
});
这里的问题是,在函数
startMenu()
上,所有按钮都按预期工作,但在我的函数startLP()
上,按钮根本没有反应。我在这里做错了什么?有人能指出吗?按钮也不适用于startCredits()
,而startGameOver()
我可以通过删除所有removeEventListener并删除函数gotoCredits和gotoHowToPlay中的addEventListener来解决此问题。但这是一种正确的做法吗?因为当你添加一个监听器时,你不应该同时删除它们吗
谢谢你你能保证这些基础知识是有效的吗?i、 e.按钮实例,帧名称…是的,实例有效,它们在几个小时前工作,但不确定我做了什么,是否还有其他方法让我检查事件是否触发?功能何时启动,
startCredits
和startGameOver
调用了吗?@Pimgd当它们的等效动作脚本帧被触发时,它们分别被调用,我有5个帧菜单:菜单、游戏、游戏结束、howtoplay、积分,每个帧都有调用等效功能的动作脚本,并且每个帧上是否存在您的按钮实例?第5帧上的脚本无法更改第1帧上对象的属性。。。至少在不使用魔法的情况下是这样。你使用的帧名称正确吗?您用大写字母引用它们,但在代码中它们是小写的。帧上的代码甚至会被调用吗?您应该插入一些trace()
statements@MartonPallagi很抱歉,这些名字的正确拼写是被称为的,而不是我写的标签。您也可以在上图中看到:)
stop();
startCredits();
stop();
var snd_scream = new SoundScream();
snd_scream.play();
var myTimer:Timer = new Timer(2000, 1);
myTimer.start();
myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, function(e:TimerEvent){
startGameOver();
});