搞砸了,如果在ActionScript 3中陈述?砸了、ActionScript

2023-09-08 12:58:22 作者:万人中央我独殇

所以,我一直在做这个游戏与动作脚本3,和CS5.5。你所要做的是避免小行星,而你在太空中飞行。我认为这将是很酷的在出太阳系行星将向下移动屏幕整个游戏的背景。还挺使它看起来像你飞通过他们。我这样做的方式是我加了五到其y坐标为每秒每帧。一旦他们的y坐标,达到600(在屏幕的底部)我想补充一个新的行星,这将做同样的事情。出于某种原因,一旦我到土星一切都变得奇怪。土星来到早,所以没有天王星。我不知道发生了什么事情。我一直感到失望,这对于一个钟头。这里就是我认为有问题的部分。

So I have been making this game with Action Script 3, and CS5.5. What you are trying to do is avoid asteroids while you fly through space. I thought it would be cool to have the planets in out solar system be moving down the screen throughout the game in the background. Kinda to make it look like you were flying pass them. The way I did this was I added five to their y coordinate each frame per second. Once their y coordinate reached 600 ( the bottom of the screen ) I would add a new planet which would do the same thing. For some reason once I got to Saturn everything got weird. Saturn came to early, and so did Uranus. I had no idea what was going on. I have been frustrated with this for a good hour. Here is the part where I think there is the problem.

公共职能onTick(timerEvent:TimerEvent):无效         {

public function onTick(timerEvent:TimerEvent):void {

        earth.PlanetMovement(5);

        if (earth.y==600)
        {
            mars.PlanetsStart(300, -100);
            addChild( mars );
            levels=levels+5;
        }
        mars.PlanetMovement(5);
        if (mars.y==600)
        {
            jupiter.PlanetsStart(300,-150);
            addChild (jupiter);
            levels=levels+10;
        }
        jupiter.PlanetMovement(5);

        if (jupiter.y==600)
        {
            saturn.PlanetsStart(300,-155);
            addChild (saturn);
            levels=levels+20;
        }
        saturn.PlanetMovement(5);
        if (saturn.y==600)
        {
            uranus.PlanetsStart(300,-160)
            addChild ( uranus);
            levels=levels+25;
        }
        uranus.PlanetMovement(5);

PlanetMovement,并PlanetsStart在行星类两种功能。如果您需要更多的信息,请告诉我。

PlanetMovement, and PlanetsStart are two functions in the Planets class. If you need more info please tell me.

编辑:我想我应该进一步解释。 PlanetsStart是一个函数,具有每个的movieclip的起始坐标。所以一旦地球达到好哦600坐标,那么火星开始于(300,-100)。然后,它被添加到屏幕上。水平是提高得分每帧每秒的变量。 PlanetMovement是多少,每一个影片剪辑将移动每帧。如果我用> =则​​分数将提高太多。

I guess I should explain further. PlanetsStart is a function that has the starting coordinate of each movieclip. So once earth reached a y coordinate of 600, then mars starts at (300, -100). Then it is added to the screen. levels is a variable that raises the score each fps. PlanetMovement is how much each movieclip will move each fps. If I were to use >= then the score would raise too much.

这是什么情况。大地显示出来,其中它应该。那么火星showd准时。然后由于某些原因土星弹出在火星的中间,和木星。在此之后土星达到使天王星出现的底部。然后木星到达底部,并一切正常,因为它应该。土星显示出来,然后天王星,以

This is exactly what happens. earth shows up where it is supposed to. Then mars showd up on time. Then for some reason Saturn pops up in the middle of mars, and Jupiter. After this Saturn reaches the bottom making Uranus appear. Then Jupiter reaches the bottom, and everything works as it should. Saturn shows up, and then Uranus in order

推荐答案

唉。不幸的是,这是不是你的暗示更复杂。这将真正帮助你先写某种类型的code计划,开发一些psuedo- code,将显示基本逻辑(和逻辑错误),并在那之后code。

Ugh. Unfortunately, this is more complex than you are implying. It would really help you to write a code plan of some sort first, develop some psuedo-code that will show basic logic (and the logic errors), and then code after that.

下面是一个更好的办法来组织这个想法的一个例子。但是,我不认为你的想法是可怕的记忆意识。您应该只引进需要的行星,并根据需要,以及删除它们。寻找到一个名为对象池,以帮助更好地构建这种技术。

Below is an example of a better way to structure this idea. However, I don't think your idea is terrible memory conscious. You should only bring in the planets as needed, and remove them as needed as well. Look into a technique called "object pooling" to help better structure this.

太阳能系统类:

package  
{
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.geom.Point;

    public class SolarSystem extends MovieClip 
    {
        private var PLANET_NAMES:Array = new Array("earth", "mars", "jupiter", "saturn", "uranus");

        // you will have to fix these values:
        private var PLANET_STARTS:Array = new Array(new Point(300, -100), new Point(300, -100),new Point(300, -100),new Point(300, -100),new Point(300, -100));

        private var planets:Array;
        private var maxY:Number = 600;
        private var speed:Number = 5;

        private var levels:Number = 0;
        private var levelIncrement:Number = 5;

        public function SolarSystem() 
        {
            super();
            addEventListener(Event.ADDED_TO_STAGE, initHnd, false, 0, true);
        }

        private function initHnd(e:Event):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, initHnd);

            runPlanets();

            addEventListener(Event.ENTER_FRAME, frameHnd, false, 0, true);
        }

        private function runPlanets():void 
        {
            for (var i:int = 0; i < PLANET_NAMES.length; i++) 
            {
                planets[i] = new Planet();
                planets[i].name = PLANET_NAMES[i];
                Planet(planets[i]).startPlanet(PLANET_STARTS[i]);
                this.addChild(planets[i]);
            }
        }

        private function frameHnd(e:Event):void 
        {
            if(planets && planets.length > 0){
                // move all the planets until they are too low, then remove them.
                // decrementing loop because planets will be being removed occasionally.
                for (var i:int = planets.length -1; i >= 0; i--) 
                {
                    if (planets[i] && Planet(planets[i]).y >= maxY) {

                        // this seems very strange to me, but it will replicate what your code says:
                        levels += (levels + levelIncrement);

                        try {
                            removeChild(Planet(planets[i]));
                            planets[i] = null;
                        }catch (e:Error) { }
                    } else if ( Planet(planets[i]).isMoving ){
                        Planet(planets[i]).movePlanet(0, speed);
                    }
                }
            } else {
                removeEventListener(Event.ENTER_FRAME, frameHnd);
            }
        }
    }
}

和这里是地球类:

package
{
    import flash.display.MovieClip;

    public class Planet extends MovieClip
    {
        private var _isMoving:Boolean = false;

        public function Planet()
        {
            super();
        }

        public function startPlanet(sx:Number, sy:Object):void
        {
            this.x = sx;
            this.y = sy;

            isMoving = true;
        }

        public function movePlanet(dx:Number, dy:Number):void
        {
            if (isMoving)
            {
                this.x += dx;
                this.y += dy;
            }
        }

        public function get isMoving():Boolean
        {
            return _isMoving;
        }

        public function set isMoving(value:Boolean):void
        {
            _isMoving = value;
        }
    }
}

再次,这是不是做到这一点的最好办法,但它更的管理的组的概念和行动纳入课程。

Again, this is not the best way to do this, but it is more manageable to group concepts and actions into classes.

HTH。