Jump to content

  • Log In with Google      Sign In   
  • Create Account


Patriarch K

Member Since 01 Nov 2012
Offline Last Active Apr 05 2013 08:11 AM

Topics I've Started

appendChild() and removeChild() problems in JS

20 March 2013 - 07:20 AM

I have a menu and from the meny I append a child called game.js which will start the game. When I complete the first stage, I want to remove the child game.js and append game2.js, but that doesn't work for me. How shall I solve this? I will show you the menu code only, where I append the child and that works fine. But later on, when I am in the game.js and press a key just for testing, then I want the current child (game.js) to be removed and I want to start the game2.js at the same time so that there will be a level change when I press a button from level 1.

 


var canvasJet = document.getElementById('canvasJet');
var ctxJet = canvasJet.getContext('2d');
var canvasBg = document.getElementById('canvasBg');
var ctxBg = canvasBg.getContext('2d');
var canvasBullet = document.getElementById('canvasBullet');
var ctxBullet = canvasBullet.getContext('2d');


var pendingImages = 1;
var menuStart = false;
var newScript = window.document.createElement("script");

var requestAnimFrame =  window.requestAnimationFrame ||
                        window.webkitRequestAnimationFrame ||
                        window.mozRequestAnimationFrame ||
                        window.msRequestAnimationFrame ||
                        window.oRequestAnimationFrame;

var music = new Audio("images/rymd2.ogg");
music.loop = true;
music.play();

var startmeny = new Image();
startmeny.src = 'images/startmeny.png';
startmeny.addEventListener('load', imageLoaded, false);

// main functions
function init() {
    loop();
    document.addEventListener('keydown', checkKeyDown, false);
}

function loop() {  
     if(menuStart == false){
     updateGame();  
     requestAnimFrame(loop);  
     }  
}

function imageLoaded()
{
    --pendingImages;
    if (pendingImages == 0)
    {
        init();
    }
}

function updateGame(){
  ctxBg.drawImage(startmeny, 0, 0); 
}

function clearCtxBg() {
    ctxBg.clearRect(0, 0, 800, 600);
}
// end of main functions

// event functions
function checkKeyDown(e) {
    var keyID = e.keyCode || e.which;
    if (keyID === 13 ) { //enter
          e.preventDefault();
          newScript.src = "game.js";
          window.document.body.appendChild(newScript); 
          music.pause();
          menuStart = true;
    }

}
// end of event functions

 

 


Creating a new level with Javascript?

13 March 2013 - 02:43 AM

I'm stuck with the idea of how to create a new level when I've completed the first one.

Of course I could use a boolean which will just change the game to level2, but it's so messy to have all the levels in the same code.

My html-file looks like below and my wish is that I could connect a variable in my game.js that tells if the level is complete. Then use an if-statement in my html code like:

 

if(game.js.levelComplete == false){

<script src="game.js"></script>

}

 

else if(game.js.levelComplete == true){

<script src="level2.js"></script>

}

 

I know that it doesn't work that way, but do you have any idea of a very easy and suitable method in this case for making a new level?

 

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Lost</title>
    <link rel="stylesheet" href="game.css">
</head>
<body>
    <section>
        <canvas id="canvasBg" width="800" height="600"></canvas>  
        <canvas id="canvasJet" width="800" height="600"></canvas>
         <canvas id="canvasBullet" width="800" height="600"></canvas>
        <script src="game.js"></script>
    </section>  
</body>
</html>

 

 


My first JS/HTML5 game (yet another space shooter)

09 March 2013 - 12:24 PM

I've been making games with Java for quite a while now but when I got the idead of putting stuff up on the web I got a lot of problems. I tried the applet thing, but that's just old and doesn't really work on any browser anymore so I had to learn a new language. Now I've been remaking one of my first Java games in JavaScript/HTML5 and it works pretty well, at least for me.

Maybe you could try and see if you get any issues or something? What browser are you using in that case? MAC/PC?

 

Shoot with space, pause with P and steer with key arrows. When you have 50 in power the boss appears and you need to kill him before you reach earth.

http://www.yffisch.com/space/

 


Help me understanding the MVC model

07 March 2013 - 02:34 PM

I've been trying to understand this model for a time now, but I don't really get it. I've seen that picture everywhere with model - control - view and yadda yadda. I understand the concept, but I don't get the thing with observers and stuff. I've tried to look at simple examples, but they are not simple enough for me I feel.

Can someone make an EXTREMELY short example of this in just a couple of rows? I don't want to have any details to make it perfect and those kind of stuff. Maybe you can just give me an example of how to change a String name = "xxx" and to change it to "yyy" with the control class file.

I want it as easy as possible and ignore every detail that makes it better and that kind of stuff. Just a basic understanding would be super! (a working code of course).

 

I would be very happy if someone could help me with this!

Thanks in advance!


How to set a timer in JavaScript?

02 March 2013 - 08:14 AM

This is not just about the timer. I wonder why it runs slow (at least on my computer). I've finally made my first JavaScript-game but I have some problems with the time. It works different on different machines. On my computer it goes very slow, but on my friends computer it goes fast as the light. When I increase the setTimeout to 100, then it goes very smooth on MY computer but then it lags on my friends computer. When I turn it to 30 it lags on MY computer but runs smooth on my friends computer.

What can be the problem? If you try my game you will probably see what it is about (key arrows and shoot with W)

http://www.yffisch.com/Rymd/index.html

 

Maybe you don't have anything to compare it with, but how am I supposed to set the "ground timer"? I haven't defined it.

 

Here is the source. It's very messy at the moment because I try to learn JavaScript:

 


 
var canvasJet = document.getElementById('canvasJet');
var ctxJet = canvasJet.getContext('2d');
var canvasBg = document.getElementById('canvasBg');
var ctxBg = canvasBg.getContext('2d');
var canvasBullet = document.getElementById('canvasBullet');
var ctxBullet = canvasBullet.getContext('2d');

var jet1 = new Jet();
var star1x = 0;
var star1y = 0;
var star1dx = -1;
var star1dy = 0;
var star2x = 0;
var star2y = 0;
var star2dx = -.5;
var star2dy = 0;
var star3x = 0;
var star3y = 0;
var star3dx = -.1;
var star3dy = 0;
var kills = 0;
var bossLife = 10;
var bossX = 200;
var bossY = 200;
var bossDX = 1;
var bossDY = 1;
var countdown = 15000;
var earthX = 800;
var earthY = 200;

var requestAnimFrame =  window.requestAnimationFrame ||
                        window.webkitRequestAnimationFrame ||
                        window.mozRequestAnimationFrame ||
                        window.msRequestAnimationFrame ||
                        window.oRequestAnimationFrame;
var die = new Audio("images/eldljud.wav");
var hit = new Audio("images/Pop.wav");
var shoot = new Audio("images/eldljud.wav");
var music = new Audio("images/rymd2.mp3");
music.loop = true;
music.play();


var background = new Image();
background.src = 'images/background.png';
background.addEventListener('load', init, false);

var plane = new Image();
plane.src = 'images/PlaneLeft.gif';
plane.addEventListener('load', init, false);

var plane2 = new Image();
plane2.src = 'images/PlaneRight.gif';
plane2.addEventListener('load', init, false);

var bullet = new Image();
bullet.src = 'images/Bullet.gif';
bullet.addEventListener('load', init, false);

var star1 = new Image();
star1.src = 'images/background3c.png';
star1.addEventListener('load', init, false);

var star2 = new Image();
star2.src = 'images/background3b.png';
star2.addEventListener('load', init, false);

var star3 = new Image();
star3.src = 'images/background3a.png';
star3.addEventListener('load', init, false);

var star1b = new Image();
star1b.src = 'images/background4c.png';
star1b.addEventListener('load', init, false);

var star2b = new Image();
star2b.src = 'images/background4b.png';
star2b.addEventListener('load', init, false);

var star3b = new Image();
star3b.src = 'images/background4a.png';
star3b.addEventListener('load', init, false);

var enemy1 = new Image();
enemy1.src = 'images/enemy.gif';
enemy1.addEventListener('load', init, false);

var enemy2 = new Image();
enemy2.src = 'images/enemy2.gif';
enemy2.addEventListener('load', init, false);

var enemy3 = new Image();
enemy3.src = 'images/enemy3.gif';
enemy3.addEventListener('load', init, false);

var boss = new Image();
boss.src = 'images/boss.gif';
boss.addEventListener('load', init, false);

var earth = new Image();
earth.src = 'images/earth.png';
earth.addEventListener('load', init, false);

// main functions
function init() {
     
    loop();
    document.addEventListener('keydown', checkKeyDown, false);
    document.addEventListener('keyup', checkKeyUp, false);
}

function loop() { 
     updateGame(); 
      setTimeout(loop,100); 
}

function updateGame(){

 ctxBg.drawImage(background, 0, 0);
       
      if(kills >= 20 && bossLife > 0){
         ctxBg.drawImage(boss, bossX, bossY);
     }
if(countdown > 0){
countdown--;
}

if(countdown <= 5000){
    ctxBg.drawImage(earth, earthX, earthY);
    if(countdown > 0){
    earthX -= 0.1;
}
}
        jet1.draw();
        jet1.move();
        jet1.spawnEnemy();
        jet1.collision();
        drawStars();
        
        if(countdown > 0){
        
        moveStar1();
        moveStar2();
        moveStar3();
       
        moveBoss();

        if(countdown <= 0){
            alert("YOU LOST! ENEMY INVADED EARTH!!");
        }
        if(bossLife <= 0){
            alert("YOU SAVED THE EARTH FROM THE EVIL SPAWN! CONGRATULATIONS!");
        }
    }
     drawText();
       // requestAnimFrame(loop);  

   
}

function drawStars(){
    if(countdown > 7500){
            // Först stjärnorna
                if(star1x > -1000){
                    ctxBg.drawImage(star1, star1x, star1y);
                }
                if(star1x < 0){
                     ctxBg.drawImage(star1, star1x+800, star1y);
                    if(star1x + 800 < 0){
                       ctxBg.drawImage(star1, star1x+1600, star1y);
                        star1x = 0;
                    }
                }
             //////  
               // Andra stjärnorna
                if(star2x > -1000){
                    ctxBg.drawImage(star2, star2x, star2y);
                }
                if(star2x < 0){
                     ctxBg.drawImage(star2, star2x+800, star2y);
                    if(star2x + 800 < 0){
                       ctxBg.drawImage(star2, star2x+1600, star2y);
                        star2x = 0;
                    }
                }
             ////// 
               // Tredje stjärnorna
                if(star3x > -1000){
                    ctxBg.drawImage(star3, star3x, star3y);
                }
                if(star3x < 0){
                     ctxBg.drawImage(star3, star3x+800, star3y);
                    if(star3x + 800 < 0){
                       ctxBg.drawImage(star3, star3x+1600, star3y);
                        star3x = 0;
                    }
                }
             ////// 
}
if(countdown <= 7500){
            // Först stjärnorna
                if(star1x > -1000){
                    ctxBg.drawImage(star1b, star1x, star1y);
                }
                if(star1x < 0){
                     ctxBg.drawImage(star1b, star1x+800, star1y);
                    if(star1x + 800 < 0){
                       ctxBg.drawImage(star1b, star1x+1600, star1y);
                        star1x = 0;
                    }
                }
             //////  
               // Andra stjärnorna
                if(star2x > -1000){
                    ctxBg.drawImage(star2b, star2x, star2y);
                }
                if(star2x < 0){
                     ctxBg.drawImage(star2b, star2x+800, star2y);
                    if(star2x + 800 < 0){
                       ctxBg.drawImage(star2b, star2x+1600, star2y);
                        star2x = 0;
                    }
                }
             ////// 
               // Tredje stjärnorna
                if(star3x > -1000){
                    ctxBg.drawImage(star3b, star3x, star3y);
                }
                if(star3x < 0){
                     ctxBg.drawImage(star3b, star3x+800, star3y);
                    if(star3x + 800 < 0){
                       ctxBg.drawImage(star3b, star3x+1600, star3y);
                        star3x = 0;
                    }
                }
             ////// 
}
}


function moveStar1(){
star1x += star1dx;
}

function moveStar2(){
star2x += star2dx;
}

function moveStar3(){
star3x += star3dx;
}

function moveBoss(){
    bossX += bossDX;
    bossY += bossDY;

if(bossX <= 0 || bossX >= 750){
    bossDX *= -1;
}
if(bossY <= 0 || bossY >= 550){
    bossDY *= -1;
}

}


function drawText(){
ctxBg.lineWidth=1;
ctxBg.fillStyle="#FFFFFF";
ctxBg.lineStyle="#ffff00";
ctxBg.font="18px sans-serif";
ctxBg.fillText("Power: " + kills, 20, 20);
ctxBg.fillText(countdown + " km to earth", 650, 20);
}


function clearCtxBg() {
    ctxBg.clearRect(0, 0, 800, 600);
}

// end of main functions
// jet functions
function Jet() {
   
    this.drawX = 420;
    this.drawY = 200;
    this.dx = 0;
    this.dy = 0;
    this.direction = 1;
    this.fire = false;
    this.isShooting = false;
    this.bullets = [];
    this.currentBullet = 0;
    this.counter = 0;
    this.enemyRand;
    this.enemyRand2;

    this.enemies = [];
    this.currentEnemy = 0;
    for (var i = 0; i < 525; i++) {
        this.bullets[this.bullets.length] = new Bullet();
    }
     for (var i = 0; i < 125; i++) {
        this.enemies[this.enemies.length] = new Enemy();
    }
}

Jet.prototype.move = function(){
    this.drawX += this.dx;
    this.drawY += this.dy;
}

Jet.prototype.draw = function() {
    clearCtxJet();
    this.fireBullet();
    this.drawAllBullets();
    this.drawAllEnemies();
    if(jet1.direction == -1){
    ctxJet.drawImage(plane, this.drawX, this.drawY);
}
   if(jet1.direction == 1){
    ctxJet.drawImage(plane2, this.drawX, this.drawY);
}
}

Jet.prototype.drawAllBullets = function() {
    for (var i = 0; i < this.bullets.length; i++) {
        if (this.bullets[i].drawX >= 0 && this.bullets[i].bulletLife > 0) {
            this.bullets[i].draw();
            this.bullets[i].move();
           
        
       
        }
    }    
}

Jet.prototype.collision = function() {
   
 for (var i = 0; i < this.bullets.length; i++) {
    for (var j = 0; j < this.enemies.length; j++) {
    // Skjuta på bossen
    if(kills >= 20){    
if(this.bullets[i].drawX > bossX && this.bullets[i].drawX < bossX+100 && this.bullets[i].drawY > bossY && this.bullets[i].drawY < bossY+90 && this.bullets[i].bulletLife > 0 && bossLife > 0){
    bossLife--;
    this.bullets[i].bulletLife--;
}
}

        // Minuspoäng när fiender är utanför skärmen
            if(this.enemies[j].drawX > -10 && this.enemies[j].drawX < 0 && this.enemies[j].outScreen == false){
kills--;
this.enemies[j].outScreen = true;
            }
// Om en bullet krockar med en fiende
            if(this.bullets[i].drawX > this.enemies[j].drawX && this.bullets[i].drawX < this.enemies[j].drawX+50 && this.bullets[i].drawY+30 > this.enemies[j].drawY && this.bullets[i].drawY < this.enemies[j].drawY+25 && this.bullets[i].bulletLife > 0 && this.enemies[j].life > 0 ){
                this.enemies[j].life--;
                if(this.enemies[j].life < 1){
                    kills++;
                }
                this.bullets[i].bulletLife--;
            }
        }
      } 
}


Jet.prototype.drawAllEnemies = function() {
    for (var i = 0; i < this.enemies.length; i++) {
        if (this.enemies[i].drawX >= 0) {
           if(this.enemies[i].life > 0){
            this.enemies[i].draw();
            this.enemies[i].move();
        }
        
       
        }
    }    
}

Jet.prototype.fireBullet = function() {
    if (this.fire == true && this.isShooting == false) {
        this.isShooting = true;
        if(this.bullets[this.currentBullet].bulletLife > 0){
            if(this.direction == 1){
        this.bullets[this.currentBullet].fire(this.drawX+95, this.drawY);
    }
         if(this.direction == -1){
        this.bullets[this.currentBullet].fire(this.drawX, this.drawY);
    }

shoot.play();
        this.bullets[this.currentBullet].isShot = true;
    }
        if(this.direction == 1){
            this.bullets[this.currentBullet].dx = 3;
        }
        if(this.direction == -1){
             this.bullets[this.currentBullet].dx = -3;
        }
        this.currentBullet++;
       
        if (this.currentBullet >= this.bullets.length) this.currentBullet = 0;
        
    } else if (this.fire == false) {
        this.isShooting = false;
    }  
}

Jet.prototype.spawnEnemy = function(){
    this.counter++;
    this.enemyRand = Math.floor((Math.random()*10000)+200);
    this.enemyRand2 = Math.floor((Math.random()*580)+1);
     if (this.counter >= this.enemyRand) {
        
        this.enemies[this.currentEnemy].fire(800, this.enemyRand2);
        this.currentEnemy++;
       
        if (this.currentEnemy >= this.enemies.length) {
            this.currentEnemy = 0;
        }
        this.counter = 0;
    }   
}


function clearCtxJet() {
    ctxJet.clearRect(0, 0, 800, 600);
    ctxBullet.clearRect(0, 0, 800, 600);
}

// end of jet functions
// bullet functions
function Bullet() {
    this.drawX = -20;
    this.drawY = 0;
    this.dx = 3;
    this.isShot = false;
    this.bulletLife = 1;

}

Bullet.prototype.move = function(){
this.drawX += this.dx;
}

Bullet.prototype.draw = function() {
   if( this.bulletLife > 0){
    ctxBullet.drawImage(bullet, this.drawX, this.drawY+25);
}
  
}

Bullet.prototype.fire = function(startX, startY) {
   
    this.drawX = startX;
    this.drawY = startY;
}

// end of bullet functions

// enemy functions
function Enemy() {
    this.drawX = -20;
    this.drawY = 0;
    this.dx;
    this.isShot = false;
    this.life = 3;
    this.outScreen = false;
}

Enemy.prototype.move = function(){
if(this.life == 3){
this.dx = 1;
}
if(this.life == 2){
this.dx = 2;
}
if(this.life == 1){
this.dx = 3;
}
this.drawX -= this.dx;
}

Enemy.prototype.draw = function() {
   
   if(this.life == 3){
    ctxBullet.drawImage(enemy1, this.drawX, this.drawY);
}
  if(this.life == 2){
    ctxBullet.drawImage(enemy2, this.drawX, this.drawY);
}
  if(this.life == 1){
    ctxBullet.drawImage(enemy3, this.drawX, this.drawY);
}


}

Enemy.prototype.fire = function(startX, startY) {
    this.drawX = startX;
    this.drawY = startY;
}

// end of enemy functions





// event functions

function checkKeyDown(e) {
    var keyID = e.keyCode || e.which;
    if (keyID === 38 ) { //up arrow
         jet1.dy = -1;    
    }
    if (keyID === 39 ) { //right arrow
      jet1.direction = 1;
        jet1.dx = 1;    
    }
    if (keyID === 40 ) { //down arrow 
        jet1.dy = 1;   

    }
    if (keyID === 37 ) { //left arrow 
          jet1.dx = -1; 
          jet1.direction = -1;
    }
    if (keyID === 87) { //spacebar
        jet1.fire = true;
       
    }
}

function checkKeyUp(e) {
    var keyID = e.keyCode || e.which;
    if (keyID === 38 ) { //up arrow 
     jet1.dy = 0; 
    }
    if (keyID === 39 ) { //right arrow 
      jet1.dx = 0; 
    }
    if (keyID === 40 ) { //down arrow 
      jet1.dy = 0; 
    }
    if (keyID === 37 ) { //left arrow 
      jet1.dx = 0; 
    }
    if (keyID === 87) { //spacebar
        jet1.fire = false;  
    }
}

// end of event functions

 

 


PARTNERS