Jump to content

Javascript game with html canvas element

- - - - -

  • Please log in to reply
1 reply to this topic

#1
madskillsmonk

madskillsmonk

    Newbie

  • Members
  • PipPip
  • 11 posts
ok so im working on a simple 2d game with javascript and canvas element, i assume i can post this issue here? Anyways, here is my javascript code that im having trouble with.... this is only a few functions out of many but these are the ones im not sure why arent working... i defined the images(fish) properly and gave it a source. this is my function call:


	   setInterval(isFishOnScreen(),50);


	

	function isFishOnScreen()

	{

		if(checkiffishisonscreen() == false)

			newFish();	

		else

			newFish();

	}

	function checkiffishisonscreen()

	{	

		if(fishyPos < 400 && fishyPos > 1)

			return false;	

		else

			return true;

	}

	function newFish()

	{	

		var numb = Math.floor(Math.random()*500);

		var x = numb;

		var dy = 5;

		context2D.drawImage(fish,numb,fishyPos);

		if(fishyPos < 400)

		{

			fishyPos += dy;

			canvas.width = canvas.width;

			context2D.drawImage(fish,x,fishyPos);

		}

	

}


so if you couldnt tell based on the code, what im trying to do is create a random fish location x-value with the random function, and draw the fish. but i only want 1 fish on the screen at a time, so i have to test if there is already a fish based on the yposition. the fish will move off the screen only by moving downward until they are off the screen, they i want to remake a new fish.


any ideas? :D

thanks in advance,
madskillsmonk

Edited by dargueta, 30 April 2011 - 06:49 PM.
Please use [code][/code] tags next time


#2
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,722 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
You can create global variables in Javascript, so just have one that indicates whether there's a fish on the screen already, and set it to false if the fish moves off of the screen so you can create another one.

By the way, your isFishOnScreen function doesn't make sense; it creates a fish regardless if one is on the screen or not.
sudo rm -rf /




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users