A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: Creating more levels problem

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Location
    India
    Posts
    28

    Creating more levels problem

    Hi
    I have level 1 on frame 1 with hitTests on some objects and if the player hits the door the game goes on to level 2 with nextFrame();

    On frame 2 I rearranged the objects from level 1 but the game somehow doesnt recognize that the objects are rearranged.
    The objects can be seen on different positions but the hittTests are still the ones from frame 1

    What did I do wrong?

    Thanks!

  2. #2
    Senior Member
    Join Date
    Mar 2010
    Posts
    107
    I can't see anything without any code. You should post your code.

  3. #3
    Junior Member
    Join Date
    Dec 2011
    Location
    India
    Posts
    28
    here's the code:

    this part is in the constructor:
    Actionscript Code:
    var level:Array = new Array();

                for(var i = 0; i<numChildren; i++){
                    if(getChildAt(i) is platform)
                    {
                        level.push(getChildAt(i).getRect(this));
                    }
                }

    addEventListener(Event.ENTER_FRAME, loop);
    addEventListener(Event.ENTER_FRAME, nextLevel);

    here's the collision test
    Actionscript Code:
    function loop(e:Event){
    if(kLeft)
                {
                    speedX = -10;
                }else if(kRight)
                {
                    speedX = 10;
                }else
                {
                    speedX*=0.5;
                }
                player.x+=speedX;
           
                for(var i = 0; i<level.length; i++)
                {
                    if(player.getRect(this).intersects(level[i]))
                    {
                        if(speedX > 0)
                        {
                            player.x = level[i].left-player.width/2;
                        }
                       

                        if(speedX < 0)
                        {
                            player.x = level[i].right+player.width/2;
                        }
                        speedX = 0;
                    }
                }
           
                speedY+=1;
                player.y+=speedY;
                var jumpable= false;
       
                for(i = 0; i<level.length; i++)
                {
                    if(player.getRect(this).intersects(level[i]))
                    {
                        if(speedY >0)
                        {
                            player.y = level[i].top-player.height/2;
                            speedY = 0;
                            jumpable= true;
                        }

                        if(speedY <0)
                        {
                            player.y = level[i].bottom+player.height/2;
                            speedY*=-0.5;
                        }
                        speedX = 0;
                    }
                    if(kUp && jumpable)
                    {
                    speedY = -15;
                    }
                }
    }

    and the function to go to the next level
    Actionscript Code:
    function nextLevel(e:Event):void{
                if(player.hitTestObject(door)){
                   nextFrame();
                }
            }

    'door' and 'player' are movieclips

  4. #4
    Senior Member
    Join Date
    Mar 2010
    Posts
    107
    I cant barely understand this code. What are you using getChild for? Why not pushing a simple sprite into the for loop? And what do you use intersects for? why not just hittest? I also dont see the point of having 2 enter frames?


    you can easlily check in the enter frame if a certain object hits another hittest and let it peform something.

  5. #5
    Junior Member
    Join Date
    Dec 2011
    Location
    India
    Posts
    28
    i was following this tutorial:

    http://www.youtube.com/watch?v=tD-t9DKvFJA

    there is one mc that is called platform and i put it into an array so i can place it more than once. then i check if the player hits the platform

    I was searching for a tutorial that uses hittests but I couldnt find one. If there is a tutorial doing it with hitTest and it works the same way, I'd really appreciate if you could give me the link
    Last edited by flip-Ed; 05-04-2012 at 08:35 AM.

  6. #6
    Senior Member
    Join Date
    Mar 2010
    Posts
    107
    I would do it like this:


    Actionscript Code:
    var platforms:Array = [];
    createPlatforms();
    addEventListener(Event.ENTER_FRAME, checkColl)


    function createPlatforms():void
    {
       for (var i:int = 0; i < 5; i++)
    {    
         var platform:Sprite = new Sprite();

          platform.graphics.beginFill(yourColor, 1);
          platform.graphics.drawRect(x,y,width,height);
          platform.graphics.endFill();
         
          addChild(platform);

         platforms.push(platform);

         platform.x = 50* i;   // position how you like it
         platform.y = 100;


    }
    }

    function checkColl(e:Event):void
    {
         for (var i:int = 0; i < platforms.length; i++)
             {
                  if (obj.hitTestObject(platforms[i]))
                 {
                    //do something
                  }

             }

    }
    Last edited by Mushrambo; 05-06-2012 at 08:38 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center