【发布时间】:2020-09-13 23:28:37
【问题描述】:
我通常是一个 python 人,虽然 JavaScript 对我来说并不是全新的,但 Phaser 游戏库是。我正在尝试使用 Phaser 制作射箭游戏,虽然我的游戏文件不完整,但据我所知,该文件至少应该加载背景图像。有人可以帮我弄清楚为什么它没有加载任何东西吗?如果有帮助,我正在使用 VSCode 的实时服务器扩展来加载它。
索引 HTML 文件
<!DOCTYPE html>
<html>
<head>
<meta charset = "UTF-8">
<title>Game</Title>
<script type = "text/javascript" src = "phaser.min.js"></script>
</head>
<body><script type="text/javascript" src="archery_game.js"></script></body>
</html>
游戏文件
const game = new Phaser.game(800, 600, Phaser.AUTO, '', {
preload: preload,
create: create,
update: update })
//Variables
let score = 0
let scoreText
let arrows
let player
function preload() {
//Load and define our images
game.load.image('archer', 'archer.png')
game.load.image('sky', 'sky.png')
game.load.image('ground', 'platform.png')
game.load.image('target', 'archery_target.png')
}
function create() {
//Implement physics
game.physics.startSystem(Phaser.physics.ARCADE)
//Add the sky sprite
game.add.sprite(0, 0, 'sky')
//Create the ground
platforms = game.add.group()
platforms.enableBody = true
let ground = platforms.create(0, game.world.height - 64, 'ground')
ground.scale.setTo(2, 2)
}
function update() {
//Update physics
}
【问题讨论】:
-
只是一个猜测——构造函数中的双引号 (
...Phaser.AUTO, '', ...) 是 HTML 中要附加到每个 Phaser 文档的父元素。我想知道,既然你把它留为空字符串,它是否没有附加到你的 HTML 画布上。 -
@DeclanMcKelvey-Hembree 我试过“game_index.html”而不是空引号,仍然没有结果:/
-
不是这样,它必须是这样的......你可以在你的 HTML 中放入像
<div id="my-game"></div>这样的 html,然后将参数字符串设置为“my-game”。根据文档,该参数应该是The DOM element into which this games canvas will be injected. Either a DOM ID (string) or the element itself.所以说“game_index.html”不符合这些要求。不过只是猜测! -
@DeclanMcKelvey-Hembree 试过了,还是不行。感谢您的尝试!
-
嗯,那我不知道。对不起!
标签: javascript phaser-framework