嗨,即時通訊全新的JavaScript,並需要help.im製作一個Html的JavaScript遊戲。我只是想問我如何讓我的敵人點擊?我已經成功地爲我的比賽創造了我的敵人,但是現在他們從遊戲畫面的頂部下來並退出底部。如果玩家接觸到任何敵人,那麼它會進入遊戲結束,但我希望玩家能夠點擊敵人,然後繼續遊戲結束。我已經嘗試了幾個星期,現在我輸了。可點擊的敵人
另外,我的播放器目前正在由鼠標控制,因爲在鼠標是播放器。
我需要改變我的collison測試嗎?即時通訊只是不知道如何讓玩家能夠點擊敵人。我需要註冊一個'點擊'功能,如onmouseclick等?
IM使用:
window.addEventListener("mousedown",onDown,false);
window.addEventListener("mousemove",onMove,false);
window.addEventListener("mouseup",onUp,false);
感謝任何幫助將是巨大的!只需要一點幫助就可以走向正確的方向。
感謝提前:)
這是當玩家移動鼠標(播放器)的功能。它作爲我的球員在鼠標movememt控制:
function onMove(e) {
if (!e) var e = window.event;
//get mouse position
var posx = 0;
var posy = 0;
if (e.pageX || e.pageY) {
posx = e.pageX;
posy = e.pageY;
}
else if (e.clientX || e.clientY) {
posx = e.clientX + document.body.scrollLeft
+ document.documentElement.scrollLeft;
posy = e.clientY + document.body.scrollTop
+ document.documentElement.scrollTop;
}
var totalOffsetX = 0;
var totalOffsetY = 0;
var currentElement = canvas;
do{
totalOffsetX += currentElement.offsetLeft;
totalOffsetY += currentElement.offsetTop;
}
while(currentElement = currentElement.offsetParent)
mouseX = posx - totalOffsetX;
mouseY = posy - totalOffsetY;
}
}
和鼠標了:
function onUp(e) {
mouseDown = false;
}
的敵人,我已經做了:
enemies = new Array();
createEnemies();
,並與功能對敵方物體(遊戲中的食物和水果物品)的動畫:
function createEnemies() {
var enemy
if(level>2 && Math.random()<0.2) {
var breakfastItems = Math.floor(Math.random() * breakfastsheets.length);
var tmpAnimation = new Animation(breakfastsheets[breakfastItems],4,2)
enemy = new Skull(tmpAnimation,Math.random()*(canvas.width-tmpAnimation.width),-tmpAnimation.height);
} else if(level>3 && Math.random()<0.2) {
var randomVegetable = Math.floor(Math.random() * vegetablesheets.length);
var tmpAnimation = new Animation(vegetablesheets[randomVegetable],4,2)
enemy = new Skull(tmpAnimation,Math.random()*(canvas.width-tmpAnimation.width),-tmpAnimation.height);
}else {
var randomFruit = Math.floor(Math.random() * enemysheets.length);
var tmpAnimation = new Animation(enemysheets[randomFruit],4,2)
enemy = new Skull(tmpAnimation,Math.random()*(canvas.width-tmpAnimation.width),-tmpAnimation.height);
}
enemy.setExplosionSound(explosionSoundPool);
enemies.push(enemy);
}
忘了說敵人的'骷髏'就是這樣:儘管我沒有使用它們,但忘記了導彈。
function Skull (image, x,y, width, height) {
//call constructor of parent object
DisplayObject.call(this,'skull', image, x,y, width, height);
//initialise objects
this.img.play();
this.img.setLoop(true);
this.img.setRange(0,4);
//private variables
var dying = false;
var alive = true;
var speed = 5;
var explosionSound;
//public methods
this.update = function(game_area, missiles) { //game area is a Rect2d, missiles is an array of display objects.
this.y+=speed;
this.img.next();
if(!dying && missiles) {
for(var i = 0; i<missiles.length; i++) {
if(Collision.test(missiles[i],this)) {
missiles[i].kill();
dying = true;
this.img.setRange(4,8);
this.img.setLoop(false);
this.img.setFrame(0);
//play explosion sound.
if(explosionSound) explosionSound.play(0.5);
}
}
}
if(Collision.isOutside(this,game_area) || (dying && !this.img.isPlaying())) {
alive = false;
}
}
//set a sound to be played when the enemy is hit.
this.setExplosionSound = function (soundPool) {
explosionSound = soundPool;
}
this.isDying = function() {
return dying;
}
this.isDead = function() {
return !alive;
}
}
Skull.prototype =新的DisplayObject();
你能分享一下更多的代碼嗎? –
當然,呃你特別想要什麼? = /我實際上並沒有創建除播放器以外的其他鼠標功能。我不能發佈這個,以及一系列的敵人?對不起,它只是完全失去了使敵人可以點擊......我還會發布我用過的collison代碼? –