Jump to content

Generating random objects in the player path


zsumair
 Share

Recommended Posts


Here's the code Lewster 

 

// Create Function

    create: function() {

        game.stage.backgroundColor='#e34221';

        

        game.physics.startSystem(Phaser.Physics.ARCADE);

        

        this.myWorld=game.add.group();

        this.myWorld.enableBody=true;

        

        //group create

        

        var ground=this.myWorld.create(0,game.world.height-64,'ground');

        ground.scale.setTo(2,2);

        ground.body.immovable=true;

        

        

        // Creating group of Boxes

        

        this.boxes = game.add.group();

        this.boxes.enableBody=true;

        this.boxes.createMultiple(20,'box');

        

        

        // Player Sprite added to the screen

                          

         this.player = game.add.sprite(0, 250,'player');

    game.physics.arcade.enable(this.player);

    this.player.body.bounce.y = 0.25;

    this.player.body.gravity.y = 980;

    this.player.body.collideWorldBounds = true;

       

        

        

        // Add animations or walk of player animations.add(name, frames, frame rate, loop);

        

        this.player.animations.add('right',[3, 4, 5],10,true);

        this.player.animations.add('left',[9, 10, 11],10,true);

        this.player.frame=6;

        

        this.cursors = this.input.keyboard.createCursorKeys();

        

        // Timer that call row of Boxes every 1.5 seconds

        this.timer=this.game.time.events.loop(1500,this.addRowOfBoxes,this);

        

    },

    

    //Update Function

    update: function() {

        game.physics.arcade.collide(this.player,this.myWorld);

        

       this.player.body.velocity.x = 0;

    if (this.cursors.right.isDown) {

      this.player.body.velocity.x = 200;

      this.player.animations.play('right');

    } else if (this.cursors.left.isDown) {

      this.player.body.velocity.x = -200;

      this.player.animations.play('left');

    } else {

      this.player.animations.stop();

      this.player.frame = 6;

    }

         // // Allow player to jump if player touching the ground.

    if (this.cursors.up.isDown && this.player.body.touching.down) {

      this.player.body.velocity.y = -500;

    }

    },

    

    //Custom Functions

    

    //Add one Box

    

    addOneBox : function(x,y){

        var box = this.boxes.getFirstDead();

        box.reset(x,y);

        box.body.velocity.x=-200;

        

        box.checkWorldBounds=true;

        box.outOfBoundsKill=true;

    },

    

    //Add row of Pipes

   addRowOfBoxes : function() {

        var hole = Math.floor(Math.random()*4);

        

        for (var i = 0; i < 8; i++)

            if (i != hole && i != hole +1) 

                this.addOneBox(470, i*50);   

      

    },

};

Link to comment
Share on other sites

  • 3 weeks later...

These forums are a fantastic resource for learning both Phaser and JavaScript in general, typically from people who are in many cases highly skilled professionals working with JavaScript full time and providing support in what little spare time they get. However as hard as we try to answer (and hopefully solve) every question that's asked, some slip the net.

 

As I said, the best way to get your problems solved is to create a test case in jsFiddle or Codepen which demonstrates just your issue clearly. If you can provide that, or if you could actually upload your game so we can see the problem happening, I'm pretty sure myself or one of the many other clued-up individuals on the forums would be able to solve it quickly, or at least give you some good steering towards the answer. Unfortunately, simply dumping in your code like this makes it difficult to work out what's going on, and most will see this and feel it'd require too much effort to turn this into a working test case to debug.

Link to comment
Share on other sites

Wow 228 views on this post.. and more than two weeks passed..but no one to help here..great community  :) not even the moderators bother to comment or even people don't even try..

 

I'd be happy to respond: You copy-pasted your code into a text field with no code highlighting and no formatting; therefore I put in as much effort as you did, none. If you copy paste code *at least* format it so we can read it. You should just give us a running example though so we can start debugging. The easier it is to help you (directly related to the effort you put in to making it easy) the more people will help you. When I see a copy-paste of a bunch of code straight into the post that looks like rubish I just move on and never look back. I had to stop this time because your above post was just so rude and entitled.

Link to comment
Share on other sites

@xerver i wasn't trying to be rude. that was just my astonishment about the replies on the forum. What you are saying seems rude. And it would be a big help to others if you **atleast** tell people to format the code or create a test case instead of just moving on.I don't think overlooking the question if you see copy pasted code can help anyone in any regard. Its just a suggestion am giving not in a rude way  :)

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...