对于一个学校项目,我必须制造一个可以投掷炸弹的移动宇宙飞船。移动部件可以工作(它会掉落一颗炸弹)但它只能在一个位置下降。如何编辑我的代码,使其落在宇宙飞船的位置?我已将大部分代码更改为英语。
var canvas;
var stage;
var image;
var theSpaceship;
var moveRight = false;
var moveLeft = false;
var moveDown = false;
var moveUp = false;
var bombPng;
var bomb;
var loadedBomb = false;
function init() {
canvas = document.getElementById("canvas");
stage = new createjs.Stage(canvas);
maakPlaatje();
document.onkeydown = handleKeyDown;
document.onkeyup = handleKeyUp;
new createjs.Ticker.setFPS(60); // frames per sec.
createjs.Ticker.addListener(window);
}
function maakPlaatje() {
image = new Image();
var url = "ruimteschip.png";
image.src = url;
image.onload = loadGraphics;
}
function loadGraphics() {
theSpaceship = new createjs.Bitmap(image);
tekenPlaatje();
}
function tekenPlaatje() {
theSpaceship.x = 0;
theSpaceship.y = 0;
stage.addChild(theSpaceship);
stage.update();
}
function loadGraphicsKogelImage() {
bomb = new createjs.Bitmap(bombPng);
bomb.scaleX = bomb.scaleY = 0.1;
bomb.x = 350;
bomb.y = 1;
stage.addChild(bomb);
stage.update();
loadedBomb = true;
console.log(rsposx);
}
function schiet() {
if (bomb == null) {
bombPng = new Image();
var url = "missile.png";
bombPng.src = url;
bombPng.name = 'popBitmap';
bombPng.onload = loadGraphicsKogelImage;
}
}
function beweegKogel() {
//eerst checken of de bomb wel bestaat
if (bomb != null) {
bomb.y = bomb.y + 5;
if (bomb.y > canvas.height) {
stage.removeChild(bomb);
bomb = null;
} // end if >canvas.height
} // end if bomb bestaat
} // end functie
function handleKeyDown(e) {
// cross browser 'e' definieren:
if (!e) {
var e = window.event;
}
if (e.keyCode == 39) {
// pijltje rechts ingedrukt.
moveRight = true;
}
if (e.keyCode == 37) {
// pijltje rechts ingedrukt.
moveLeft = true;
}
if (e.keyCode == 38) {
// pijltje rechts ingedrukt.
moveUp = true;
}
if (e.keyCode == 40) {
// pijltje rechts ingedrukt.
moveDown = true;
}
if (e.keyCode == 32) {
// spatiebalk ingedrukt.
schiet();
} else {
console.log("welke toets? maar het nummer is " + e.keyCode);
}
}
function handleKeyUp(e) {
// cross browser 'e' definieren:
if (!e) {
var e = window.event;
}
if (e.keyCode == 39) {
moveRight = false;
}
if (e.keyCode == 37) {
moveLeft = false;
}
if (e.keyCode == 40) {
moveDown = false;
}
if (e.keyCode == 38) {
moveUp = false;
}
}
var speed = 10;
function tick() {
stage.update();
if (theSpaceship != null) {
// indien moveRight true is, loopt het plaatje:
if (moveRight)
theSpaceship.x = theSpaceship.x + speed;
if (moveLeft)
theSpaceship.x = theSpaceship.x - speed;
if (moveDown)
theSpaceship.y = theSpaceship.y + speed;
if (moveUp)
theSpaceship.y = theSpaceship.y - speed;
if (theSpaceship.x > canvas.width)
theSpaceship.x = 0;
if (theSpaceship.y > canvas.height)
theSpaceship.y = 0;
if (loadedBomb)
beweegKogel();
}
}