如何解决我目前正在 Phaser 2 的学校项目中工作我有运动代码,但玩家无法与地图的第一层发生碰撞
main.js
:
const Game = new Phaser.Game(1152,608,Phaser.AUTO,'game-canvas',{ preload,create,update })
function preload() {
Game.load.spritesheet('player','WalkAnim-768x22 128x1.png',384/8,384/3)
Game.load.tilemap('map','NewMap.json',null,Phaser.Tilemap.TILED_JSON)
Game.load.image('Sprite-0003','Sprite-0003.png')
}
function create() {
mapC()
playerC()
Game.physics.startSystem(Phaser.Physics.ARCADE);
Game.physics.enable([p],Phaser.Physics.ARCADE);
p.body.collideWorldBounds = true
Game.camera.follow(p,Phaser.Camera.FOLLOW_PLATFORMER,1,1)
}
let fance
function mapC() {
map = Game.add.tilemap('map',32,32)
map.addTilesetimage('Sprite-0003')
map.setCollisionByExclusion([])
fance = map.createLayer(0)
layer = map.createLayer(1)
fance.setScale(1)
layer.resizeWorld()
}
function playerC() {
p = Game.add.sprite(100,100,"player")
a1 = p.animations.add("a",[16,17,18,19,20,21,22,23],10,true)
a2 = p.animations.add("b",[0,2,3,4,5,6,7],true)
a3 = p.animations.add("c",[8,9,11,12,13,14,15],true)
p.scale.setTo(0.5)
p.anchor.setTo(0.5)
}
function update() {
// p.body.setSize(30,32)
// p.body.offset.y = 48 * 2
//Game.debug.body(p)
Game.physics.arcade.collide(p,fance,TheChec)
if (Game.input.keyboard.isDown(Phaser.Keyboard.W)) {
p.animations.play("a");
p.body.veLocity.y = -200
p.body.veLocity.x = 0
} else if (Game.input.keyboard.isDown(Phaser.Keyboard.S)) {
p.body.veLocity.y = 200
p.body.veLocity.x = 0
p.animations.play("c");
} else if (Game.input.keyboard.isDown(Phaser.Keyboard.A)) {
p.body.offset.x = 8
p.body.veLocity.x = -200
p.animations.play("b");
p.scale.setTo(-0.5,0.5)
p.body.veLocity.y = 0
} else if (Game.input.keyboard.isDown(Phaser.Keyboard.D)) {
p.body.offset.x = 8
p.body.veLocity.x = 200
p.animations.play("b");
p.scale.setTo(0.5)
p.body.veLocity.y = 0
} else {
p.body.veLocity.y = 0
p.body.veLocity.x = 0
p.animations.stop()
}
}
function TheChec() {
console.log('It happaned')
}
index.html
:
<!DOCTYPE html>
<html>
<head>
<title>Game</title>
</head>
<body>
<div>
<div id='game-canvas'></div>
</div>
<script src='https://cdnjs.cloudflare.com/ajax/libs/phaser-ce/2.12.0/phaser.js'></script>
<script src='main.js'></script>
</body>
</html>
NewMap.json
:
{ "compressionlevel":-1,"height":50,"infinite":false,"layers":[
{
"data":[25,25,25],"id":2,"name":"Tile Layer 2","opacity":1,"type":"tilelayer","visible":true,"width":50,"x":0,"y":0
},{
"data":[23,23,30,29,24,29],"id":1,"name":"Tile Layer 1","y":0
}],"nextlayerid":3,"nextobjectid":1,"orientation":"orthogonal","renderorder":"right-down","tiledversion":"1.4.3","tileheight":32,"tilesets":[
{
"columns":6,"firstgid":1,"image":"..\/InUse\/Sprite-0003.png","imageheight":160,"imagewidth":192,"margin":0,"name":"Sprite-0003","spacing":0,"tilecount":30,"tiles":[
{
"id":11,"properties":[
{
"name":"collides","type":"bool","value":true
}]
},{
"id":12,{
"id":13,{
"id":17,{
"id":18,{
"id":19,{
"id":22,{
"id":23,{
"id":28,{
"id":29,"value":true
}]
}],"tilewidth":32
}],"tilewidth":32,"type":"map","version":1.4,"width":50
}
我上传了行走动画和图块集的精灵:
抱歉我的字典不好!:)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。