Jump to content

Can't draw sprite with gravity properly


Recommended Posts

Ok, so I've noticed my game draws 2 sprites.. One with no physics enabled in the foreground.. And the second with gravity working, but it's behind my game background.


I've also noticed that when I implemented the gravity, it didn't use any X/Y coordinates, how do I make it change them?

var game = new Phaser.Game(1024, 512 , Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, render: render, update: update });var player;var background1 = {    x: 0,    y: 0};var background2 = {    x: 1024,    y: 0};var playerX = 100;var playerY = 40;function preload(){    game.load.image('playerImage', 'img/player.png');    game.load.image('backgroundImage', 'img/background.png');}function create(){    game.physics.startSystem(Phaser.Physics.ARCADE);    //  Set the world (global) gravity    game.physics.arcade.gravity.y = 350;    //  Sprite 1 will use the World (global) gravity    game.add.sprite(background1);    game.add.sprite(background2);    player = game.add.sprite(100, 96, 'playerImage');    // Enable physics on those sprites    game.physics.enable(player, Phaser.Physics.ARCADE);    player.body.collideWorldBounds = true;    player.body.bounce.y = 0.1;    player.body.gravity.y = 200;}function render(){    game.world.removeAll();    game.add.sprite(background1.x, background1.y, 'backgroundImage');    game.add.sprite(background2.x, background2.y, 'backgroundImage');    game.add.sprite(playerX, playerY, 'playerImage');}function jump(){}function moveRight(){}function moveLeft(){}function updateBackground(){    background1.x--;    background2.x--;    if(background1.x <= -1024)    {        background1.x = 1024;    }    if(background2.x <= -1024)    {        background2.x = 1024;    }}function update(){    updateBackground();}
Link to comment
Share on other sites


  • Recently Browsing   0 members

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