在很多次失败后,我无法理解为什么碰撞在玩家精灵和图腾图像之间不起作用。我重复使用Phaser.io教程中的相同代码进行双重检查,但无法找到collide
和overlap
的问题。
目标是在玩家到达此测试游戏中的图腾时取消隐藏文字:http://likeglue.org/game/
以下是代码:
//Game settings
var game = new Phaser.Game(320, 240, Phaser.AUTO, '', { preload: preload, create: create, update: update });
function preload() {
//Load game assets
game.load.image('ground', 'assets/ground.png');
game.load.image('totem', 'assets/totem.png');
game.load.spritesheet('player', 'assets/player.png', 18, 18);
game.load.bitmapFont('carrier_command', 'assets/carrier_command.png', 'assets/carrier_command.xml');
}
var player;
var totem;
var platforms;
var cursors;
var totemSay;
var text;
function create() {
// scale the game 2x
game.scale.scaleMode = Phaser.ScaleManager.USER_SCALE;
game.scale.setUserScale(2, 2);
// enable crisp rendering
game.renderer.renderSession.roundPixels = true;
Phaser.Canvas.setImageRenderingCrisp(this.game.canvas);
// We're going to be using physics, so enable the Arcade Physics system
game.physics.startSystem(Phaser.Physics.ARCADE);
// background for our game
game.stage.backgroundColor = '#000000';
// The platforms group contains the ground
platforms = game.add.group();
// We will enable physics for any object that is created in this group
platforms.enableBody = true;
//Create ground
//var ground = platforms.create(0, game.world.height - 32, 'ground');
var ground = game.add.tileSprite(0, game.world.height - 32, game.world.width, game.world.height, 'ground');
//Add tileset to group
platforms.add(ground);
// This stops it from falling away when you jump on it
ground.body.immovable = true;
//Create totem
var totem = game.add.image(220, game.world.height - 56, 'totem');
game.physics.arcade.enable(totem);
// The player and its settings
player = game.add.sprite(32, game.world.height - 50, 'player');
// We need to enable physics on the player
game.physics.arcade.enable(player);
// Player physics properties. Give the little guy a slight bounce.
player.body.bounce.y = 0.2;
player.body.gravity.y = 500;
player.body.collideWorldBounds = true;
// Our two animations, walking left and right.
player.animations.add('left', [3, 4], 10, true);
player.animations.add('right', [1, 2], 10, true);
// Our controls.
cursors = game.input.keyboard.createCursorKeys();
//Text
text = "Hello \r\n stranger.";
totemSay = game.add.bitmapText(-15, -20, 'carrier_command', text, 5);
totem.addChild(totemSay);
totemSay.align = 'center';
totemSay.fixedToCamera = true;
totemSay.visible = false;
}
function update() {
// Collide the player and the stars with the platforms
game.physics.arcade.collide(player, platforms);
//Overlapping player with totem
game.physics.arcade.collide(player, totem, collisionHandler, null, this);
// Reset the players velocity (movement)
player.body.velocity.x = 0;
if (cursors.left.isDown)
{
// Move to the left
player.body.velocity.x = -150;
player.animations.play('left');
}
else if (cursors.right.isDown)
{
// Move to the right
player.body.velocity.x = 150;
player.animations.play('right');
}
else
{
// Stand still
player.animations.stop();
player.frame = 0;
}
// Allow the player to jump if they are touching the ground.
/*if (cursors.up.isDown && player.body.touching.down)
{
player.body.velocity.y = -250;
}*/
}
function collisionHandler (player, totem) {
// Totem talk
totemSay.visible = true;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title> PyPlatformer </title>
<script type="text/javascript" src="phaser.min.js"></script>
<script type="text/javascript" src="main.js"></script>
</head>
<body>
</body>
</html>
我这里最接近我的问题的主题是这个thread,不知何故它不起作用:/
答案 0 :(得分:0)
尝试在声明时删除var platforms
行。
重叠函数为game.physics.arcade.overlap(....)
希望它有所帮助。
答案 1 :(得分:0)
而不是写
var totem = game.add.image(220, game.world.height - 56, 'totem');
使用
totem = game.add.image(220, game.world.height - 56, 'totem');
当您编写关键字var
时,它会创建一个只能由create
方法访问的局部变量。删除var
关键字会使其随处可访问。
如果它仍然不起作用,那么你可能需要将你的图腾转换为sprite
对象并为该对象启用物理。我是移相器的新手,我不确定图像是否有物理实体可以让它们进行物理操作。