我一直在尝试在Flash中创建一个游戏,如果你按下某个数字和字母组合,它会弹出一个额外的菜单。我这部分遇到了麻烦。每当我测试它时,它将调出场景并在它们之间快速切换。如果有人可以帮助解决这个问题我会很高兴。返回第一个场景的选项尚不包括在内。
我的代码:
package {
import flash.display.MovieClip;
import flash.text.TextField;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class DocumentMain extends MovieClip {
public var _konamiCodeHandler:Number = 0;
public var _bounces:TextField;
public var _highscore:TextField;
public var _gravityText:TextField;
public var _ball:Ball;
public var _resume:Resume;
public var _cheatMenu:CheatMenu;
private var _vx:Number;
private var _vy:Number;
public const GRAVITY:Number = 2;
public const BOUNCE_FACTOR:Number = 0.8;
public const HIT_FORCE:Number = 20;
public function DocumentMain():void {
_vx = 10;
_vy = 0;
stop();
_cheatMenu = new CheatMenu;
_cheatMenu.visible = false;
_ball.buttonMode = true;
addEventListener(Event.ENTER_FRAME, enterFrameHandler);
addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
addEventListener(KeyboardEvent.KEY_DOWN, onKeyPressed);
}
private function enterFrameHandler(e:Event):void {
// gravitate ball
_vy += GRAVITY;
// move ball
_ball.x += _vx;
_ball.y += _vy;
// check stage boundaries
checkBoundaryCollision();
}
private function mouseDownHandler(e:MouseEvent):void {
// hit ball on click
if (e.target == _ball) {
hit(e.target.mouseX, e.target.mouseY);
}
if (e.target == _cheatMenu) {
gotoAndStop(1, "Scene 2");
}
}
private function onKeyPressed(e:KeyboardEvent):void {
if (e.keyCode == Keyboard.UP && _konamiCodeHandler == 0) {
_konamiCodeHandler = 1;
} else if (e.keyCode == Keyboard.UP && _konamiCodeHandler == 1) {
_konamiCodeHandler = 2;
} else if (e.keyCode == Keyboard.DOWN && _konamiCodeHandler == 2) {
_konamiCodeHandler = 3;
} else if (e.keyCode == Keyboard.DOWN && _konamiCodeHandler == 3) {
_konamiCodeHandler = 4;
} else if (e.keyCode == Keyboard.LEFT && _konamiCodeHandler == 4) {
_konamiCodeHandler = 5;
} else if (e.keyCode == Keyboard.RIGHT && _konamiCodeHandler == 5) {
_konamiCodeHandler = 6;
} else if (e.keyCode == Keyboard.LEFT && _konamiCodeHandler == 6) {
_konamiCodeHandler = 7;
} else if (e.keyCode == Keyboard.RIGHT && _konamiCodeHandler == 7) {
_konamiCodeHandler = 8;
} else if (e.keyCode == Keyboard.B && _konamiCodeHandler == 8) {
_konamiCodeHandler = 9;
} else if (e.keyCode == Keyboard.A && _konamiCodeHandler == 9) {
_cheatMenu.visible = true;
_cheatMenu.buttonMode = true;
_konamiCodeHandler = 0;
} else {
_konamiCodeHandler = 0;
}
}
private function checkBoundaryCollision():void {
var left:Number;
var right:Number;
var top:Number;
var bottom:Number;
left = _ball.x - (_ball.width / 2);
right = _ball.x + (_ball.width / 2);
top = _ball.y - (_ball.height / 2);
bottom = _ball.y + (_ball.height / 2);
if (left < 0 && _vx < 0) {
_ball.x = (_ball.width / 2);
_vx *= -BOUNCE_FACTOR;
} else if (right > stage.stageWidth && _vx > 0) {
_ball.x = stage.stageWidth - (_ball.width / 2);
_vx *= -BOUNCE_FACTOR;
}
if (top < 0 && _vy < 0) {
_ball.y = (_ball.height / 2);
_vy *= -1;
} else if (bottom > stage.stageHeight && _vy > 0) {
_ball.y = stage.stageHeight - (_ball.height / 2);
_vy *= -BOUNCE_FACTOR;
if (Number(_bounces.text) > Number(_highscore.text)) {
_highscore.text = _bounces.text
}
_bounces.text = "0";
}
}
private function hit(hitX:Number, hitY:Number):void {
// increment bounces
_bounces.text = String(Number(_bounces.text) + 1);
// adjust vertical velocity of ball
if (_vy > 0) {
_vy *= -BOUNCE_FACTOR / 2;
}
_vy -= HIT_FORCE;
// adjust horizontal velocity of ball
if (_vx * hitX > 0) {
_vx *= -BOUNCE_FACTOR;
}
_vx -= (2 * hitX / _ball.width) * HIT_FORCE;
}
}
}
这就是我得到的错误:
TypeError:错误#1034:类型强制失败:无法将flash.display :: MovieClip @ 7fffe980281转换为CheatMenu。 在flash.display :: Sprite / constructChildren() 在flash.display :: Sprite() 在flash.display :: MovieClip() 在DocumentMain()
答案 0 :(得分:0)
一些明显的问题:gotoAndStop(0, "Scene 2");
似乎错了,因为没有第0帧。你的意思是:gotoAndStop(1, "Scene 2");
您也可以对变量_cheatMenu
进行说明,但是您永远不会实例化“_cheatMenu”或将其添加到显示列表中。我认为这就是你的错误#1034所说的。
您应该在构造函数方法中添加cheatMenu = new CheatMenu;
,然后将其初始化为:cheatMenu.visible = false
,假设您希望它在开始时不可见。
帧之间的快速切换(场景?)听起来像stop();
语句在某处丢失。
这可能无法解决您的所有问题,但它可以帮助您实现目标。