主页 > canvas引擎 > Phaser游戏引擎 >
来源:未知 时间:2021-07-03 23:20 作者:小飞侠 阅读:次
[导读] varconfig={type:Phaser.WEBGL,width:800,height:600,backgroundColor:#2d2d2d,parent:phaser-example,pixelArt:true,scene:{preload:preload,create:create,update:update}};vargame=newPhaser.Game(config);varcontrols;varmap;functionpreload(){this.load...
|
var config = {
type: Phaser.WEBGL,
width: 800,
height: 600,
backgroundColor: '#2d2d2d',
parent: 'phaser-example',
pixelArt: true,
scene: {
preload: preload,
create: create,
update: update
}
};
var game = new Phaser.Game(config);
var controls;
var map;
function preload ()
{
this.load.image('tiles', 'assets/tilemaps/tiles/tmw_desert_spacing.png');
this.load.tilemapTiledJSON('map', 'assets/tilemaps/maps/desert.json');
}
function create ()
{
map = this.make.tilemap({ key: 'map' });
var tiles = map.addTilesetImage('Desert', 'tiles');
var layer = map.createLayer('Ground', tiles, 0, 0);
// 设置相机边界和地图边界保持一致
this.cameras.main.setBounds(0, 0, map.widthInPixels, map.heightInPixels);
// 创建并返回一个包含上、下、左、右4个热键的对象,还有空格键和shift键。
var cursors = this.input.keyboard.createCursorKeys();
var controlConfig = {
camera: this.cameras.main,
left: cursors.left,
right: cursors.right,
up: cursors.up,
down: cursors.down,
speed: 0.5
};
// 创建操作相机配置
controls = new Phaser.Cameras.Controls.FixedKeyControl(controlConfig);
var help = this.add.text(16, 16, 'Click a tile to replace all instances with a plant.', {
fontSize: '18px',
padding: { x: 10, y: 5 },
backgroundColor: '#000000',
fill: '#ffffff'
});
help.setScrollFactor(0);
}
function update (time, delta)
{
// 需要实时更新
controls.update(delta);
// 获得当前活动输入实列
if (this.input.manager.activePointer.isDown)
{
// 获取一个Camera并返回一个Vector2,其中包含这个Pointer的转换位置 在这个相机。这可以用来转换这个指针的位置到相机空间。
var worldPoint = this.input.activePointer.positionToCamera(this.cameras.main);
// 从给定层获取给定世界坐标处的贴图。 如果没有指定图层,则使用当前图层映射。
var tile = map.getTileAtWorldXY(worldPoint.x, worldPoint.y);
// 这将用植物(瓷砖id=38)替换选定瓷砖的所有实例。
map.replaceByIndex(tile.index, 38);
//也可以在特定区域内替换(tileX、tileY、width、height):
//map.replaceByIndex(tile.index,38,5,5,15,15);
//也可以在特定区域内替换(tileX、tileY、width、height):
//map.replaceByIndex(tile.index,38,5,5,15,15);
}
} |
自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习
京ICP备14009008号-1@版权所有www.zixuephp.com
网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com