Jump to content

creating platforms via an array need help making them immovable


bowlofrice
 Share

Recommended Posts

Hi,
I am working on a phaser project in where the platforms are randomly generated. I followed: 

 

but now I'm having an issue making the platforms immovable, getting an undefined error:

         

               platforms = game.add.group();
              platforms.enableBody = true;

                for(var i = 0; i < 15; i++){
               createUniqueLocation();

          }
            platforms.sort();

 

                function createUniqueLocation(){

                          do{
                                var x = game.math.snapTo(game.world.randomX, 32) / 32;
                                var y = game.math.snapTo(game.world.randomY, 32) / 32;

                            if(y > 750){
                                  y = 500;
                               }
                                 var idx = (y * 17) + x;

                              }
                                while(locs.indexOf(idx) !== -1);
                               locs.push(idx);
                            platforms.create(x * 32, y * 32, 'platform', game.rnd.integerInRange(0,7));

                           }

 

I saw something sort of similar where they made a function outside of the phaser canvas, whats the best way to make the platforms immovable?

 

Thanks

Link to comment
Share on other sites

So I have tried a few more things:

Now I'm not getting an undefined error, but immovable = true is still not working.

I have added in a ingame timer to run to allow all the platforms in the array to be created. However, the platforms are still ignoring body.immovable = true;

 

Quote

              function createUniqueLocation(){

                     do{
                            var x = game.math.snapTo(game.world.randomX, 32) / 32;
                            var y = game.math.snapTo(game.world.randomY, 32) / 32;

                       if(y > 750){
                             y = 500;
                     }
                           var idx = (y * 17) + x;

      }
                  while(locs.indexOf(idx) !== -1);
                  locs.push(idx)
                 platforms.create(x * 32, y * 32, 'platform', game.rnd.integerInRange(0,7))

            }        

                  function createPlatforms(){
                      //adding immovable to the array of platforms.
                        locs.forEach(function(e){
                
                           e.body.immovable = true;
                       }, this);
                  }
        
                   function inGameTime(){
                     timeCheck = game.time.now; 
                 }    
under

              function update(){

               if(game.time.now - timeCheck > 2000){
                 createPlatforms()
                  } else {
                  console.log(locs);
               }

}


 

 

 

Link to comment
Share on other sites

I resolved this myself.

made the platform creation into a variable which then allowed me to add the attribute:

 

while(locs.indexOf(idx) !== -1);
                  locs.push(idx)
               var platformCreate = platforms.create(x * 32, y * 32, 'platform', game.rnd.integerInRange(0,7));

            }        

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...