-1

正如标题所说,我是 AS3/Flash 的新手,正在努力学习如何制作游戏。我一直在关注 Gary Rosenzweig 的“Flash Game University”(本书和 iTunes 播客)的教程。作为免责声明,我使用的所有代码都来自上述教程,我没有创建它们。我只是将它们拼接在一起以尝试使某些东西以不同的方式工作。

在发布代码之前,让我简要解释一下我的问题以及我为解决它而尝试做的事情。基本上,当我完全使用教程代码(它使用“catcher.x = mouseX”进行移动)时,我让游戏运行良好,但我想尝试让玩家(“catcher”)左右移动箭头键代替鼠标。然而,当我把它放进去时,运动变得不稳定,而不是平滑地左右移动,玩家左右跳跃,不断加快速度,直到它直接跳出屏幕。不用说,我不知道为什么会这样。

起初,我认为基于箭头的移动的代码是错误的,所以我将它复制并粘贴到一个单独的文件中进行测试,它运行良好,所以我认为出于某种原因,使游戏其余部分工作的代码是以某种方式打破它。

我尝试将代码移动到不同的地方,但这并没有解决问题(事实上,它经常使问题变得更糟)。

我想我在格式化我的代码方面遗漏了一些东西(不幸的是,我发现的教程都没有涵盖 AS3 的正确样式,而且我之前没有任何编码经验,所以我的代码可能是令人作呕的混乱/无组织)。

作为最后一点,我提前为我的代码的混乱道歉我对此很陌生,只是想弄清楚事情。

非常感谢任何帮助,感谢您花时间阅读本文!

package {
    import flash.display.*;
    import flash.events.*;
    import flash.utils.Timer;
    import flash.utils.getDefinitionByName;


    public class CatchingGame1 extends MovieClip {
        var catcher:Catcher;
        var nextObject:Timer;
        var objects:Array = new Array;
        const speed:Number = 7.0;
        var score:int = 0;
        var leftArrow:Boolean = false;
        var rightArrow:Boolean = false;

        public function CatchingGame1() {
            catcher = new Catcher();
            catcher.y = 350;
            addChild(catcher);
            setNextObject();
            addEventListener(Event.ENTER_FRAME,moveObjects);

        }

        public function setNextObject() {
            nextObject = new Timer(Math.random()*1000,1);
            nextObject.addEventListener(TimerEvent.TIMER_COMPLETE,newObject);
            nextObject.start();
        }

        public function newObject(e:Event) {
            var goodObjects:Array = ["Circle1","Circle2"];
            var badObjects:Array = ["Square1","Square2"];
            if(Math.random() < .5) {
                var r:int = Math.floor(Math.random()*goodObjects.length);
                var classRef:Class = getDefinitionByName(goodObjects[r]) as Class;
                var newObject:MovieClip = new classRef();
                newObject.typestr = "good";
            } else {
                r = Math.floor(Math.random()*badObjects.length);
                classRef = getDefinitionByName(badObjects[r]) as Class;
                newObject = new classRef();
                newObject.typestr = "bad";
            }
            newObject.x = Math.random()*500;
            addChild(newObject);
            objects.push(newObject);
            setNextObject();
        }

        public function moveObjects(e:Event) {
            for(var i:int=objects.length-1; i>=0;i--) {
                objects[i].y += speed;
                if(objects[i].y > 400) {
                    removeChild(objects[i]);
                    objects.splice(i,1);
                }
                if(objects[i].hitTestObject(catcher)) {
                    if(objects[i].typestr == "good") {
                        score += 5;
                    } else {
                        score -= 1;
                    }
                    if(score < 0) score = 0;
                    scoreDisplay.text = "Score: " + score;
                    removeChild(objects[i]);
                    objects.splice(i,1);

                }
            }
            //this is where it starts to have problems

            //make arrow booleans true
        stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressedDown);
        stage.addEventListener(KeyboardEvent.KEY_UP, keyPressedUp);
        stage.addEventListener(Event.ENTER_FRAME, moveCatcher);
            function keyPressedDown(Event:KeyboardEvent){
                if (Event.keyCode == 37){
                    leftArrow = true;
                } else if (Event.keyCode == 39){
                    rightArrow = true;
                }

            }

            function keyPressedUp(event:KeyboardEvent) {
                if(event.keyCode == 37){
                    leftArrow = false;
                } else if(event.keyCode == 39){
                    rightArrow = false;
                }
            }
                function moveCatcher(event:Event) {
                    var catcherSpeed:Number = 1;
                    if(leftArrow){
                        catcher.x -= catcherSpeed;
                    } 
                    if(rightArrow){
                        catcher.x += catcherSpeed;
                    }
                }



        }

    }
}
4

1 回答 1

0

将添加的 keyUp、keyDown 事件处理程序移动到您的构造函数中。

不要使用多个 ENTER_FRAME 事件处理程序,只需在 moveObjects 函数中调用 moveCatcher 即可。

您的 moveObjects 方法末尾似乎缺少 },我对其进行了编辑。看看是否有帮助。

package {
import flash.display.*;
import flash.events.*;
import flash.utils.Timer;
import flash.utils.getDefinitionByName;


public class CatchingGame1 extends MovieClip {
    var catcher:Catcher;
    var nextObject:Timer;
    var objects:Array = new Array;
    const speed:Number = 7.0;
    var score:int = 0;
    var leftArrow:Boolean = false;
    var rightArrow:Boolean = false;

    public function CatchingGame1() {
        catcher = new Catcher();
        catcher.y = 350;
        addChild(catcher);
        setNextObject();

        stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressedDown);
        stage.addEventListener(KeyboardEvent.KEY_UP, keyPressedUp);

        addEventListener(Event.ENTER_FRAME,moveObjects);

    }

    public function setNextObject() {
        nextObject = new Timer(Math.random()*1000,1);
        nextObject.addEventListener(TimerEvent.TIMER_COMPLETE,newObject);
        nextObject.start();
    }

    public function newObject(e:Event) {
        var goodObjects:Array = ["Circle1","Circle2"];
        var badObjects:Array = ["Square1","Square2"];
        if(Math.random() < .5) {
            var r:int = Math.floor(Math.random()*goodObjects.length);
            var classRef:Class = getDefinitionByName(goodObjects[r]) as Class;
            var newObject:MovieClip = new classRef();
            newObject.typestr = "good";
        } else {
            r = Math.floor(Math.random()*badObjects.length);
            classRef = getDefinitionByName(badObjects[r]) as Class;
            newObject = new classRef();
            newObject.typestr = "bad";
        }
        newObject.x = Math.random()*500;
        addChild(newObject);
        objects.push(newObject);
        setNextObject();
    }

    public function moveObjects(e:Event) 
    {
        moveCatcher(null);
        for(var i:int=objects.length-1; i>=0;i--)  
        {
            objects[i].y += speed;
            if(objects[i].y > 400)
            {
                removeChild(objects[i]);
                objects.splice(i,1);
            }
            if(objects[i].hitTestObject(catcher)) 
            {
                if(objects[i].typestr == "good") 
                {
                    score += 5;
                } else {
                    score -= 1;
                }
                if(score < 0) score = 0;
                scoreDisplay.text = "Score: " + score;
                removeChild(objects[i]);
                objects.splice(i,1);

            }
        }
    }

        function keyPressedDown(Event:KeyboardEvent){
            if (Event.keyCode == 37){
                leftArrow = true;
            } else if (Event.keyCode == 39){
                rightArrow = true;
            }

        }

        function keyPressedUp(event:KeyboardEvent) {
            if(event.keyCode == 37){
                leftArrow = false;
            } else if(event.keyCode == 39){
                rightArrow = false;
            }
        }
            function moveCatcher(event:Event) {
                var catcherSpeed:Number = 1;
                if(leftArrow){
                    catcher.x -= catcherSpeed;
                } 
                if(rightArrow){
                    catcher.x += catcherSpeed;
                }
            }



    }

}
}
于 2012-05-08T14:28:53.600 回答