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

#5045349 appendChild() and removeChild() problems in JS

Posted by Patriarch K on 21 March 2013 - 01:39 PM

At the moment I use booleans in the menu called level1 = false and so on. I wrap these booleans around the loop-function in every game file and it works. If I use the google debugging it doesn't cost too much. Is this a good method or is it awkward? It works fine at the moment at least.

 

Is there a way to run a function only once and then quit it afterwards in my main script without touching the game.js?

For example, if I do like this:

 

KEY PRESS ENTER{

switch(level)

case 1:

level1();

break;

 

case 2:

level2();

etc etc....

 

If I have it like that in the main function then it will of course run the level you pick. But when you've completed that level. Then that level has already started.

The same if i put the switch statements in the loop. Then it will run multiple times (or only one time if I use a boolean on it). But I still can't quit it.

 

Is this something I have to do in the level-files?




#5042638 Creating a new level with Javascript?

Posted by Patriarch K on 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>

 

 




#5037090 How to redefine variable in Javascript

Posted by Patriarch K on 27 February 2013 - 05:06 AM

Yes of course, it was a fatal and clumsy mistake to write setDX = 1. Of course it is supposed to be setDX(1);

That works just fine! Thanks for seeing that.

 

I have a new question and I can maybe use this thread for that question too.

I want to make my bullet array but I don't really get it. If it was Java, then I would have done:

ArrayList<Bullet> bullets = new ArrayList<Bullet>();

And then:

for(Bullet b : bullets){

drawImage(b.getImage(), b.getX(), b.getY());

b.move();

}

And then when pressing the fire button I would have done:

bullets.add(new Bullet(x,y));

 

I don't really get how to do this in JavaScript. My bullet code looks kind of exactly like my Jet code above here in this thread. Do you have an idea of how to do this in JavaScript?




#5036478 How to redefine variable in Javascript

Posted by Patriarch K on 25 February 2013 - 04:08 PM

I have a strange problem with javascript at the moment. I'm so much used to Java so I get very frustated when it doesn't work out in the same way. I have a function Jet that is a plane which I try to move. Then I have the x += dx; in my move-function as usual.

dx is 0 at the beginning. When I change it to 1 or 2 or something it works fine and my Jet moves, but I want to start with 0 and then I want to change dx to 1 when pressing the keyboard. That is no problem at all in Java, but this I don't understand. Nothing happens...

At the top I have 

var jet1 = Jet();

 

function Jet()
{
   var x = 200;
   var y = 300;
   var dx = 0;
   var dy = 0;

   var Jet = function()
   {
   }
   Jet.prototype.draw = function(){
     jet1.update();
     ctxJet.drawImage(plane, x, y);
      
   }

 Jet.prototype.setDX = function(speed)
   {
       dx = speed;    
   }

   Jet.prototype.getX = function()
   {
      return x;
   }
   
   Jet.prototype.getY = function()
   {
      return y;
   }
   
   Jet.prototype.update = function(){
    ctxJet.clearRect(0, 0, 800, 600);
   }

   Jet.prototype.move = function()
   {
      x += dx;
      y += dy;
      return this;
   }
   return new Jet();
}

// And here is the keyboard which doesn't work (the keyboard input works, but not the variable change)

function checkKeyDown(e) {
    var keyID = e.keyCode || e.which;
    if (keyID === 38 ) { //up arrow     
        jet1.setDX = 1;
        jet1.dx = 1;    
    }
}

 

How am I supposed to solve this? I can't reach and change the variable.




#5033895 Help moving a picture with JavaScript

Posted by Patriarch K on 18 February 2013 - 03:26 PM

I solved the problem myself, no worries!




PARTNERS