Jessicaward25 Posted August 3, 2015 Share Posted August 3, 2015 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 More sharing options...
Skeptron Posted August 3, 2015 Share Posted August 3, 2015 I'm pretty sure you should not removeAll() at each render() call... It looks like you keep on destroy your player and adding it to the scene at the initial place. Thus he will never fall because of gravity. Link to comment Share on other sites More sharing options...
Jessicaward25 Posted August 3, 2015 Author Share Posted August 3, 2015 Hi, thanks for replying When I comment out removeAll() it starts to lag.. I'm guessing this is because of the amount of sprites on the screen at once. Did you mean to replace removeAll() with something else? If so, do you know what it is? Link to comment Share on other sites More sharing options...
Skeptron Posted August 3, 2015 Share Posted August 3, 2015 It lags? Did you give us all the code or is there more to it? What device are you testing on? Link to comment Share on other sites More sharing options...
Jessicaward25 Posted August 3, 2015 Author Share Posted August 3, 2015 Yeah that's all the code And I'm testing it on my laptop, Intel Core i7, 6GB ram, Intel HD 4000 Link to comment Share on other sites More sharing options...
Skeptron Posted August 3, 2015 Share Posted August 3, 2015 Could you put your code into a Phaser sandbox, or JS Fiddle or anything that could help running / debugging it? I can't really figure out where the big bug is, but there obviously is one if it lags with so few lines of code. EDIT : what is the size of your images? Link to comment Share on other sites More sharing options...
Recommended Posts