1
我對JavaScript有點新,並且最近一直在和Phaser一起玩。所以我正在建造一個無限的側面卷軸,除了我的玩家不會與牆壁碰撞,一切正常。這兩個精靈都啓用了物理功能,我嘗試了多種解決方案,但其中沒有一個能夠工作。你能幫我解決嗎?Sprite vs Group Collider不能在移相器中使用enableBody設置爲true
function bloxo()
{
\t var game = new Phaser.Game(1200, 600, Phaser.CANVAS, 'gameStage', { preload: preload, create: create, update: update });
\t var prevHole = 3;
\t function preload() {
\t \t game.load.image('bloxoDown','../bloxo/assets/images/bloxoDown.png');
\t \t game.load.image('bloxoUp','../bloxo/assets/images/bloxoUp.png');
\t \t game.load.image('wall','../bloxo/assets/images/platform.png',400,200);
\t \t var space;
\t \t var esc;
\t \t var player;
\t \t var walls;
\t \t var score;
\t }
\t function create() {
\t \t //Canvas With a White Bacground and Physics is Created
\t \t game.stage.backgroundColor = "#ffffff";
\t \t game.physics.startSystem(Phaser.Physics.ARCADE);
\t \t //Sets the initial Score.
\t \t score = 0; \t
\t \t //Sets how fast the tiles move
\t \t tileSpeed = -300;
\t \t tileWidth = game.cache.getImage('wall').width;
\t \t tileHeight = game.cache.getImage('wall').height;;
\t \t //Keys for User Input are created
\t \t space = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
\t \t esc = game.input.keyboard.addKey(Phaser.Keyboard.ESC);
\t \t //Adds Bloxo to the game as a sprite.
\t \t player = game.add.sprite(200,200,'bloxoDown');
\t \t player.scale.setTo(0.6, 0.6);
\t \t game.physics.enable(player, Phaser.Physics.ARCADE);
\t \t player.body.collideWorldBounds = true;
\t \t player.body.immovable = true;
\t \t //Walls Group is created
\t \t walls = game.add.physicsGroup();
\t \t walls.createMultiple(50, 'wall');
\t \t walls.enableBody = true;
\t \t game.physics.arcade.overlap(player, walls,null,this)
\t \t
\t \t game.physics.arcade.collide(player,walls,gameOver);
\t \t // Stop the following keys from propagating up to the browser
\t \t game.input.keyboard.addKeyCapture([ Phaser.Keyboard.SPACEBAR, Phaser.Keyboard.ESC,]);
\t \t //Unpausing Function
\t \t window.onkeydown = function(event)
\t {
\t if (esc.onDown && (esc.timeDown > 2000))
\t { \t
\t \t if(game.paused)
\t \t {
\t \t game.paused = !game.paused;
\t \t pauseLbl.destroy();
\t \t }
\t }
\t }
\t //Add an initial platform
\t \t addWall();
\t \t //Add a platform every 3 seconds
\t \t var timerWorld = game.time.events.loop(500, addWall);
\t }
\t function update() {
\t \t if (space.isDown)
\t {
\t player.body.y -=5;
\t bloxoUp();
\t }
\t else
\t {
\t \t player.body.y +=5;
\t \t bloxoDown();
\t }
\t if(esc.isDown)
\t {
\t \t pauseGame(); \t
\t }
\t }
\t function bloxoUp()
\t {
\t \t player.loadTexture('bloxoUp');
\t }
\t function bloxoDown()
\t {
\t \t player.loadTexture('bloxoDown');
\t }
\t function pauseGame()
\t {
\t \t game.paused = true;
\t \t pauseLbl = game.add.text(500, 300, 'Game Paused', { font: '30px Roboto', fill: '#aaaaaa' });
\t }
\t function addTile(x,y)
\t {
\t //Get a tile that is not currently on screen
\t var tile = walls.getFirstDead();
\t
\t //Reset it to the specified coordinates
\t tile.reset(x,y);
\t tile.body.velocity.x = tileSpeed;
\t tile.body.immovable = true;
\t
\t //When the tile leaves the screen, kill it
\t tile.checkWorldBounds = true;
\t tile.outOfBoundsKill = true;
\t }
\t function addWall()
\t {
\t //Speed up the game to make it harder
\t tileSpeed -= 1;
\t score += 1;
\t
\t //Work out how many tiles we need to fit across the whole screen
\t var tilesNeeded = Math.ceil(game.world.height/tileHeight);
\t //Add a hole randomly somewhere
\t do
\t {
\t \t var hole = Math.floor(Math.random() * (tilesNeeded - 2)) + 1;
\t \t }while((hole > (prevHole + 2)) && (hole < (prevHole - 2)));
\t \t prevHole = hole;
\t //Keep creating tiles next to each other until we have an entire row
\t //Don't add tiles where the random hole is
\t for (var i = 0; i < tilesNeeded; i++){
\t if (i != hole && (i != hole+1 && i != hole-1) && (i != hole+2 && i != hole-2)){
\t addTile(game.world.width, i * tileHeight);
\t }
\t }
\t }
\t function gameOver()
\t {
\t \t console.log("player hit");
\t \t player.kill();
\t \t game.state.start(game.state.current);
\t }
}
我會吻你,如果我可以。非常感謝,我不明白爲什麼它不能工作幾個小時,因爲我正在研究的教程說碰撞是在創建函數中。再次非常感謝! –