Javascript 在宇宙飞船的位置发射炸弹
为了一个学校项目,我必须制造一个可以投掷炸弹的移动太空船。运动部件工作(它投下炸弹),但它只在一个位置上落下。我如何编辑我的代码,使其落在宇宙飞船的位置?我把大部分代码改成了英语Javascript 在宇宙飞船的位置发射炸弹,javascript,easeljs,Javascript,Easeljs,为了一个学校项目,我必须制造一个可以投掷炸弹的移动太空船。运动部件工作(它投下炸弹),但它只在一个位置上落下。我如何编辑我的代码,使其落在宇宙飞船的位置?我把大部分代码改成了英语 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 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();
}
}
炸弹的起始位置应该是宇宙飞船的位置,你似乎已经将其硬编码为350,1。PS你可能想在你的宇宙飞船下面启动它,以避免把自己炸飞。谢谢你的回复。我知道我是这么做的,只是想看看它是否有效。当我放置bomb.x=spaceship.x时,什么也没发生,我真的不知道如何让它工作。天哪。。。。。应该是太空船。我怎么可能没看到?!对不起,伙计们。。已经解决了。