Jump to content

Camera Follow Sprite


i3Designer
 Share

Recommended Posts

I have: 
var w = window.innerWidth;
var h = window.innerHeight;            
 
var game = new Phaser.Game(w, h, Phaser.AUTO, 'Mio videogioco', { preload:preload, create:create, update:update });
 

I use the arcade physics but I can not get the camera to follow my sprite "box" 
the box moves only vertically 
So the box.velocity.x = -15; 
I used example.phaser.io but I can not

 

Link to comment
Share on other sites

Without seeing more of the code we cannot say what the problem is. One unrelated issue you may have is using 'Mio videogioco' as the parent - this should really be the id attribute of where you want the game to appear on the page, which cannot contain spaces. Something like this:

<div id="gamecontainer"></div>
var game = new Phaser.Game(w, h, Phaser.AUTO, 'gamecontainer', { preload: preload, create: create, update: update });
Link to comment
Share on other sites

function create() {

game.add.tileSprite(0, 0, w, 2000, 'sfondo');

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

game.physics.arcade.gravity.y=500;

player = game.add.sprite(150, 320, 'player');
player.events.onInputDown.add(jump)
game.physics.p2.enable(player);

game.camera.follow(player);

game.physics.enable (player);

}

 

function jump(){

player.body.velocity.y=-15;

}

 

I want the camera follows the player

Link to comment
Share on other sites

Camera can't go out of bounds. Since you put the height and width of the world to be equal to the window's ,the camera finds the end of world (which is the screen, let's say 800x600) and does not move. Try increasing the bounds of the world and see what changes.

game.world.setBounds(0, 0, w, 2000);
Link to comment
Share on other sites

I resolved

function create() {
    game.physics.startSystem(Phaser.Physics.ARCADE);
    game.world.setBounds(0,0,0,2000);
    //Settiamo la gravità
    game.physics.arcade.gravity.y=500;
    
    sfondo= game.add.sprite(0,0,'bg');
    sfondo.anchor.setTo(0,0);
    sfondo.inputEnabled=true;
    sfondo.events.onInputDown.add(salto);
    
    
    box = game.add.sprite(150,300,'box');
    box.anchor.setTo(0.5,0.5);
    game.physics.enable(box);
    game.camera.follow(box);
}
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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