Jump to content

Check out our Community Blogs

Register and join over 40,000 other developers!

Recent Status Updates

View All Updates

- - - - -

Javascript game with html canvas element

HTML element

  • Please log in to reply
1 reply to this topic

#1 madskillsmonk


    CC Newcomer

  • Just Joined
  • PipPip
  • 11 posts

Posted 23 April 2011 - 12:11 PM

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:


	function isFishOnScreen()
		if(checkiffishisonscreen() == false)
	function checkiffishisonscreen()
		if(fishyPos < 400 && fishyPos > 1)
			return false;	
			return true;
	function newFish()
		var numb = Math.floor(Math.random()*500);
		var x = numb;
		var dy = 5;
		if(fishyPos < 400)
			fishyPos += dy;
			canvas.width = canvas.width;

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,

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

  • 0

#2 dargueta


    I chown trolls.

  • Moderator
  • 4854 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, Perl, Assembly, Bash, Others
  • Learning:Objective-C

Posted 30 April 2011 - 06:53 PM

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.
  • 0

sudo rm -rf / && echo $'Sanitize your inputs!'

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download