我在Flash as3中创建了一个迷宫游戏,它作为一个单独的SWF都很棒。一旦我将它放入主容器swf,它就会停止工作!嗯,它仍然有点作用,但它失去了迷宫的“界限”。有谁知道我怎么能解决这个问题吗?任何帮助都非常适合,S。
package {
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.display.*;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.geom.Rectangle;
import gs.TweenLite;
import gs.easing.*;
public class slide17 extends MovieClip {
private var maze:mazeBG;
private var mazeC:mazeCircle;
var rightArrow:Boolean = false;
var leftArrow:Boolean = false;
var upArrow:Boolean = false;
var downArrow:Boolean = false;
var speed:int = 10;
public function slide17():void {
screen01();
}
private function screen01():void {
stage.addEventListener(KeyboardEvent.KEY_DOWN, stage_onKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, stage_onKeyUp);
stage.addEventListener(Event.ENTER_FRAME, stage_onEnterFrame);
maze = new mazeBG();
addChild(maze);
maze.x=88;
maze.y=155;
mazeC = new mazeCircle();
addChild(mazeC);
mazeC.x=104;
mazeC.y=321;
function stage_onKeyDown(event:KeyboardEvent):void {
if(event.keyCode == Keyboard.RIGHT) rightArrow = true;
if(event.keyCode == Keyboard.LEFT) leftArrow = true;
if(event.keyCode == Keyboard.UP) upArrow = true;
if(event.keyCode == Keyboard.DOWN) downArrow = true;
}
function stage_onKeyUp(event:KeyboardEvent):void {
if(event.keyCode == Keyboard.RIGHT) rightArrow = false;
if(event.keyCode == Keyboard.LEFT) leftArrow = false;
if(event.keyCode == Keyboard.UP) upArrow = false;
if(event.keyCode == Keyboard.DOWN) downArrow = false;
}
function stage_onEnterFrame(event:Event):void {
var rect:Rectangle = mazeC.getBounds(stage);
var i:int = 0;
var xBump:int = 0;
var yBump:int = 0;
if(rightArrow) {
xBump = speed;
for(i = 0; i < speed; i++) {
if(maze.hitTestPoint(rect.right + i, mazeC.y, true)) {
xBump = i - 1;
break;
}
}
}
if(leftArrow) {
xBump = -speed;
for(i = 0; i < speed; i++) {
if(maze.hitTestPoint(rect.left - i, mazeC.y, true)) {
xBump = -i + 1;
break;
}
}
}
if(upArrow) {
yBump = -speed;
for(i = 0; i < speed; i++) {
if(maze.hitTestPoint(mazeC.x, rect.top - i, true)) {
yBump = -i + 1;
break;
}
}
}
if(downArrow) {
yBump = speed;
for(i = 0; i < speed; i++) {
if(maze.hitTestPoint(mazeC.x, rect.bottom + i, true)) {
yBump = i - 1;
break;
}
}
}
mazeC.x += xBump;
mazeC.y += yBump;
}
}
}
}
答案 0 :(得分:3)
你的专栏:
var rect:Rectangle = mazeC.getBounds(stage);
使用舞台对象获取MazeCircle的区域。当您将游戏添加到包装器swf(主容器swf)时,舞台对象现在是包装器的阶段。如果包装器的尺寸与游戏swf不同,则对getBounds()
的调用将返回时髦的坐标。
您需要的是一些一致的测量方法,当舞台对象发生变化时不会改变。有几种方法可以解决这个问题:
使用Sprite
或MovieClip
打包您的游戏,并将其与getBounds()
而不是舞台一起使用。您可以使用slide_17
关键字来使用您工作的this
实例。例如:
var rect:Rectangle = mazeC.getBounds(this);
使用getBounds()
以外的功能来确定MazeCircle的区域,例如,使用属性x
,y
,width
和{ {1}}而是。如果您想要一个这样的例子,请告诉我,但我打赌你可以弄明白 - 将它包含在这里会很漫长。
希望这有帮助!