thebest07111
Gebruiker
- Lid geworden
- 28 nov 2011
- Berichten
- 64
hallo
Ik ben bezig met een top down shooter
Ik heb nu dat er steeds 1 soort vliegtuig naar beneden komt
Maar ik wil dat er 5 enemeys naar beneden komen
met de naam enemy.png enemy1.png enemy2.png enemy3.png enemy4.png
Deze moeten ook allemaal gewoon de hittest hebben die al in het script staat. Ook moet die gewoon tellen dat er een max is.
Nu staat er max 5 vliegtuigen dit moet ook gebeuren als er meerdere zijn. dus bijvoorbeeld 1 van elk plaatje
Ik zou alleen niet weten hoe ik dit moet doen.
Kan iemand mij hiermee helpen.
Hier is mijn code[js]//enemies
var enemy;
var enemyTotal = 5;
var enemies = [];
var enemy_x = 50;
var enemy_y = -45;
var enemy_w = 50;
var enemy_h = 50;
var speed = 3;
//Build enemy array of x/y coordinate, width, height, and speed
for (var i = 0; i < enemyTotal; i++) {
enemies.push([enemy_x, enemy_y, enemy_w, enemy_h, speed]);
enemy_x += enemy_w + 60;
}
//Iterate through array of enemies and draw them on the canvas
function drawEnemies() {
for (var i = 0; i < enemies.length; i++) {
ctx.drawImage(enemy, enemies[0], enemies[1]);
}
}
//Iterate through array of enemies and update their position
function moveEnemies() {
for (var i = 0; i < enemies.length; i++) {
if (enemies[1] < height) {
enemies[1] += enemies[4];
} else if (enemies[1] > height - 1) {
//after the enemies scroll off screen, this value is where they get re-spawned
enemies[1] = -45;
}
}
}
//Check for laser collision
function hitTest() {
var remove = false;
for (var i = 0; i < lasers.length; i++) {
for (var j = 0; j < enemies.length; j++) {
//laser's y position is less than or equal to the enemy's y position plus its height -AND-
//laser's x position is greater than or equal to the enemy's x position -AND-
//laser's x position is less than or equal to the enemy's x position plus it's width
if (lasers[1] <= (enemies[j][1] + enemies[j][3]) && lasers[0] >= enemies[j][0] && lasers[0] <= (enemies[j][0] + enemies[j][2])) {
remove = true;
//draw();//hier explosion afspelen
enemies.splice(j, 1); //enemy at this array position no longer gets drawn
enemies.push([(Math.random() * 500) + 50, -45, enemy_w, enemy_h, speed]); //refill a new enemy in a random x position between 50 and 500
}
}
if (remove == true) {
lasers.splice(i, 1); //laser at this array position no longer gets drawn
remove = false;
score += 10;
}
}
}
[/js]
en hier worden de images aangemaakt
[js]//wereld
var canvas;
var ctx;
var width = 600;
var height = 600;
var starfield;
var starX = 0;
var starY = 0;
var starY2 = -600;
//scoring
var score = 0;
var alive = true;
var lives = 3;
var gameStarted = false;
image = new Image()
image.src = 'explosion.png';
//Clear the canvas, this is required before each canvas frame update
function clearCanvas() {
ctx.clearRect(0, 0, width, height);
}
//Initialize image objects, canvas, and event listeners
function init() {
canvas = document.getElementById('canvas');
ctx = canvas.getContext('2d');
enemy = new Image();
enemy.src = 'enemy.png';
ship = new Image();
ship.src = 'ship.png';
starfield = new Image();
starfield.src = 'starfield.jpg';
document.addEventListener('keydown', keyDown, false);
document.addEventListener('keyup', keyUp, false);
canvas.addEventListener('click', gameStart, false);
gameLoop();
}
//Used by an event listener. Removes the overall canvas listener once game has started.
function gameStart() {
gameStarted = true;
canvas.removeEventListener('click', gameStart, false);
}
//Game loop that calls all the functions needed to be updated
function gameLoop() {
clearCanvas();
drawStarfield();
if (alive && gameStarted && lives > 0) {
hitTest();
moveEnemies();
moveLaser();
drawEnemies();
drawShip();
drawLaser();
shipCollision();
}
scoreTotal();
//game = setTimeout(gameLoop, 1000 / 30); //not sure why 'game =' is included...
setTimeout(gameLoop, 1000 / 30); //this works too...
}
//Used to draw several text and shape objects for score, lives, and pre-game information.
function scoreTotal() {
ctx.font = 'bold 18px VT323';
ctx.fillStyle = '#fff';
ctx.fillText('Score: ', 10, 55);
ctx.fillText(score, 70, 55);
ctx.fillText('Lives: ', 10, 30);
ctx.fillText(lives, 68, 30);
if (!alive) {
ctx.fillText('Game Over!', 245, (height / 2));
//Continue button
ctx.fillRect((width / 2) - 65, (height / 2) + 10, 100, 40);
ctx.fillStyle = '#000';
ctx.fillText('Opnieuw?', 252, (height / 2) + 35);
canvas.addEventListener('click', continueButton, false);
}
//Pre-Game Text
if (!gameStarted) {
ctx.font = 'bold 50px VT323';
ctx.fillText('Sander Gouman top down', (width / 2) - 210, (height / 2));
ctx.font = 'bold 20px VT323';
ctx.fillText('Klik om te spelen', (width / 2) - 56, (height / 2) + 30);
ctx.fillText('Beweeg met de pijltjes', (width / 2) - 100, (height / 2) + 60);
ctx.fillText('Gebruik de spatiebalk om te schieten', (width / 2) - 100, (height / 2) + 90);
}
}
//Used by an event listener. Detects if mouse click is within a specific area.
function continueButton(e) {
var cursorPos = getCursorPos(e);
if (cursorPos.x > (width / 2) - 53 && cursorPos.x < (width / 2) + 47 && cursorPos.y > (height / 2) + 10 && cursorPos.y < (height / 2) + 50) {
alive = true;
lives = 3;
reset();
canvas.removeEventListener('click', continueButton, false);
}
}
//Used for detection of mouse click on canvas
function getCursorPos(e) {
var x;
var y;
if (e.pageX || e.pageY) {
x = e.pageX;
y = e.pageY;
} else {
x = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
y = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
}
x -= canvas.offsetLeft;
y -= canvas.offsetTop;
var cursorPos = new cursorPosition(x, y);
return cursorPos;
}
//Store x and y cursor position
function cursorPosition(x, y) {
this.x = x;
this.y = y;
}
//Check lives
function checkLives() {
lives -= 1;
if (lives > 0) {
reset();
} else if (lives == 0) {
alive = false;
}
}
//Reset enemies and player back to default position
function reset() {
var enemy_reset_x = 50;
ship_x = (width / 2) - 25;
ship_y = height - 75;
ship_w = 50;
ship_h = 57;
for (var i = 0; i < enemies.length; i++) {
enemies[0] = enemy_reset_x;
enemies[1] = -45;
enemy_reset_x = enemy_reset_x + enemy_w + 60;
}
}
//Draw a continuous, scrolling starfield (using two instances)
function drawStarfield() {
ctx.drawImage(starfield, starX, starY);
ctx.drawImage(starfield, starX, starY2);
if (starY > 600) {
starY = -599;
}
if (starY2 > 600) {
starY2 = -599;
}
starY += 1;
starY2 += 1;
}
//Initialize canvas when page is loaded
window.onload = init;
[/js]
Ik ben bezig met een top down shooter
Ik heb nu dat er steeds 1 soort vliegtuig naar beneden komt
Maar ik wil dat er 5 enemeys naar beneden komen
met de naam enemy.png enemy1.png enemy2.png enemy3.png enemy4.png
Deze moeten ook allemaal gewoon de hittest hebben die al in het script staat. Ook moet die gewoon tellen dat er een max is.
Nu staat er max 5 vliegtuigen dit moet ook gebeuren als er meerdere zijn. dus bijvoorbeeld 1 van elk plaatje
Ik zou alleen niet weten hoe ik dit moet doen.
Kan iemand mij hiermee helpen.
Hier is mijn code[js]//enemies
var enemy;
var enemyTotal = 5;
var enemies = [];
var enemy_x = 50;
var enemy_y = -45;
var enemy_w = 50;
var enemy_h = 50;
var speed = 3;
//Build enemy array of x/y coordinate, width, height, and speed
for (var i = 0; i < enemyTotal; i++) {
enemies.push([enemy_x, enemy_y, enemy_w, enemy_h, speed]);
enemy_x += enemy_w + 60;
}
//Iterate through array of enemies and draw them on the canvas
function drawEnemies() {
for (var i = 0; i < enemies.length; i++) {
ctx.drawImage(enemy, enemies[0], enemies[1]);
}
}
//Iterate through array of enemies and update their position
function moveEnemies() {
for (var i = 0; i < enemies.length; i++) {
if (enemies[1] < height) {
enemies[1] += enemies[4];
} else if (enemies[1] > height - 1) {
//after the enemies scroll off screen, this value is where they get re-spawned
enemies[1] = -45;
}
}
}
//Check for laser collision
function hitTest() {
var remove = false;
for (var i = 0; i < lasers.length; i++) {
for (var j = 0; j < enemies.length; j++) {
//laser's y position is less than or equal to the enemy's y position plus its height -AND-
//laser's x position is greater than or equal to the enemy's x position -AND-
//laser's x position is less than or equal to the enemy's x position plus it's width
if (lasers[1] <= (enemies[j][1] + enemies[j][3]) && lasers[0] >= enemies[j][0] && lasers[0] <= (enemies[j][0] + enemies[j][2])) {
remove = true;
//draw();//hier explosion afspelen
enemies.splice(j, 1); //enemy at this array position no longer gets drawn
enemies.push([(Math.random() * 500) + 50, -45, enemy_w, enemy_h, speed]); //refill a new enemy in a random x position between 50 and 500
}
}
if (remove == true) {
lasers.splice(i, 1); //laser at this array position no longer gets drawn
remove = false;
score += 10;
}
}
}
[/js]
en hier worden de images aangemaakt
[js]//wereld
var canvas;
var ctx;
var width = 600;
var height = 600;
var starfield;
var starX = 0;
var starY = 0;
var starY2 = -600;
//scoring
var score = 0;
var alive = true;
var lives = 3;
var gameStarted = false;
image = new Image()
image.src = 'explosion.png';
//Clear the canvas, this is required before each canvas frame update
function clearCanvas() {
ctx.clearRect(0, 0, width, height);
}
//Initialize image objects, canvas, and event listeners
function init() {
canvas = document.getElementById('canvas');
ctx = canvas.getContext('2d');
enemy = new Image();
enemy.src = 'enemy.png';
ship = new Image();
ship.src = 'ship.png';
starfield = new Image();
starfield.src = 'starfield.jpg';
document.addEventListener('keydown', keyDown, false);
document.addEventListener('keyup', keyUp, false);
canvas.addEventListener('click', gameStart, false);
gameLoop();
}
//Used by an event listener. Removes the overall canvas listener once game has started.
function gameStart() {
gameStarted = true;
canvas.removeEventListener('click', gameStart, false);
}
//Game loop that calls all the functions needed to be updated
function gameLoop() {
clearCanvas();
drawStarfield();
if (alive && gameStarted && lives > 0) {
hitTest();
moveEnemies();
moveLaser();
drawEnemies();
drawShip();
drawLaser();
shipCollision();
}
scoreTotal();
//game = setTimeout(gameLoop, 1000 / 30); //not sure why 'game =' is included...
setTimeout(gameLoop, 1000 / 30); //this works too...
}
//Used to draw several text and shape objects for score, lives, and pre-game information.
function scoreTotal() {
ctx.font = 'bold 18px VT323';
ctx.fillStyle = '#fff';
ctx.fillText('Score: ', 10, 55);
ctx.fillText(score, 70, 55);
ctx.fillText('Lives: ', 10, 30);
ctx.fillText(lives, 68, 30);
if (!alive) {
ctx.fillText('Game Over!', 245, (height / 2));
//Continue button
ctx.fillRect((width / 2) - 65, (height / 2) + 10, 100, 40);
ctx.fillStyle = '#000';
ctx.fillText('Opnieuw?', 252, (height / 2) + 35);
canvas.addEventListener('click', continueButton, false);
}
//Pre-Game Text
if (!gameStarted) {
ctx.font = 'bold 50px VT323';
ctx.fillText('Sander Gouman top down', (width / 2) - 210, (height / 2));
ctx.font = 'bold 20px VT323';
ctx.fillText('Klik om te spelen', (width / 2) - 56, (height / 2) + 30);
ctx.fillText('Beweeg met de pijltjes', (width / 2) - 100, (height / 2) + 60);
ctx.fillText('Gebruik de spatiebalk om te schieten', (width / 2) - 100, (height / 2) + 90);
}
}
//Used by an event listener. Detects if mouse click is within a specific area.
function continueButton(e) {
var cursorPos = getCursorPos(e);
if (cursorPos.x > (width / 2) - 53 && cursorPos.x < (width / 2) + 47 && cursorPos.y > (height / 2) + 10 && cursorPos.y < (height / 2) + 50) {
alive = true;
lives = 3;
reset();
canvas.removeEventListener('click', continueButton, false);
}
}
//Used for detection of mouse click on canvas
function getCursorPos(e) {
var x;
var y;
if (e.pageX || e.pageY) {
x = e.pageX;
y = e.pageY;
} else {
x = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
y = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
}
x -= canvas.offsetLeft;
y -= canvas.offsetTop;
var cursorPos = new cursorPosition(x, y);
return cursorPos;
}
//Store x and y cursor position
function cursorPosition(x, y) {
this.x = x;
this.y = y;
}
//Check lives
function checkLives() {
lives -= 1;
if (lives > 0) {
reset();
} else if (lives == 0) {
alive = false;
}
}
//Reset enemies and player back to default position
function reset() {
var enemy_reset_x = 50;
ship_x = (width / 2) - 25;
ship_y = height - 75;
ship_w = 50;
ship_h = 57;
for (var i = 0; i < enemies.length; i++) {
enemies[0] = enemy_reset_x;
enemies[1] = -45;
enemy_reset_x = enemy_reset_x + enemy_w + 60;
}
}
//Draw a continuous, scrolling starfield (using two instances)
function drawStarfield() {
ctx.drawImage(starfield, starX, starY);
ctx.drawImage(starfield, starX, starY2);
if (starY > 600) {
starY = -599;
}
if (starY2 > 600) {
starY2 = -599;
}
starY += 1;
starY2 += 1;
}
//Initialize canvas when page is loaded
window.onload = init;
[/js]
Laatst bewerkt door een moderator: