Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-04-2012, 03:45 PM   PM User | #1
TechIlliterate
New to the CF scene

 
Join Date: Nov 2012
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
TechIlliterate is an unknown quantity at this point
Control Movement of Obeject with Arrow Keys and WASD

The following code should allow the user to control the movement of the "Hydralisk" gif with the arrow and WASD keys, but nothing happens upon pressing either. I've gone through the code several times and can't find an error.

I'm very new to Javascript, so please explain what I did wrong if it's anything major.

Thanks!

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Phase 3</title>
	
	<style>
		
		#Hydralisk {position:absolute; left:200px; top:200px}
		
	</style>
	
	<script>
		var timer
		var leftPressed = false
		var rightPressed = false
		var upPressed = false
		var downPressed = false
		var myTop = 200
		var myLeft = 200
		var movement = 10
			
		function startMovementCycle()
		{
		timer=setInterval('moveHydralisk()',50)
		}
		
		function checkIfPressed(event)
			{	
			if (event.keyCode==37 || event.keyCode==65)	{leftPressed = true}		
			if (event.keyCode==38 || event.keyCode==87)	{upPressed = true}
			if (event.keyCode==39 || event.keyCode==68)	{rightPressed = true}
			if (event.keyCode==40 || event.keyCode==83)	{downPressed = true}
			
			}
			
		function checkIfNotPressed(event)				
			{	
			if (event.keyCode==37 || event.keyCode==65)	{leftPressed = false}				
			if (event.keyCode==38 || event.keyCode==87)	{upPressed = false}
			if (event.keyCode==39 || event.keyCode==68)	{rightPressed = false}
			if (event.keyCode==40 || event.keyCode==83)	{downPressed = false}		
			}
			
		function moveHydralisk()
			{
			if (upPressed = true)			{myTop-=movement; document.getElementById('Hydralisk').style.top=myTop+"px"}
			if (downPressed = true)			{myTop+=movement; document.getElementById('Hydralisk').style.top=myTop+"px"}
			if (leftPressed = true)			{myLeft-=movement; document.getElementById('Hydralisk').style.left=myLeft+"px"}
			if (rightPressed = true)		{myLeft+=movement; document.getElementById('Hydralisk').style.left=myLeft+"px"}
			}
	
	</script>
</head>

<body onload="startMovementCycle()" onkeydown="checkIfPressed(event)" onkeyup="checkIfNotPressed(event)">

	<div id="Hydralisk"><img src="Images/Hydralisk.gif" /></div>

</body>
</html>
TechIlliterate is offline   Reply With Quote
Old 12-04-2012, 05:23 PM   PM User | #2
DanInMa
Senior Coder

 
DanInMa's Avatar
 
Join Date: Nov 2010
Location: Salem,Ma
Posts: 1,307
Thanks: 12
Thanked 204 Times in 204 Posts
DanInMa is on a distinguished road
you arent calling moveHydralisk() from your keyup and keydown functions, thats what sticks out to me anyway.
__________________
- Firebug is a web developers best friend! - Learn it, Love it, use it!
- Validate your code! - JQ/JS troubleshooting
- Using jQuery with Other Libraries - Jslint for Jquery/other JS library users
DanInMa is offline   Reply With Quote
Old 12-05-2012, 09:43 AM   PM User | #3
donna1
New Coder

 
Join Date: Nov 2012
Location: london
Posts: 55
Thanks: 5
Thanked 1 Time in 1 Post
donna1 can only hope to improve
No thats not the problem because move function is being called by the timer.

PROBLEM IS should be == not assignment =
when you check if (upPressed == true) etc

Ive just tested it with that changed and its works!

btw Why lack of ; at the ends of lines?

Also would be nice if you thanked me for fixing that because I working towards 'earning' my avatar and no one has thanked me for anything yet!

Last edited by donna1; 12-05-2012 at 10:21 AM..
donna1 is offline   Reply With Quote
Users who have thanked donna1 for this post:
TechIlliterate (12-06-2012)
Old 12-05-2012, 10:53 AM   PM User | #4
donna1
New Coder

 
Join Date: Nov 2012
Location: london
Posts: 55
Thanks: 5
Thanked 1 Time in 1 Post
donna1 can only hope to improve
After I fixed it I loaded it to my site so you can try
http://donoi.hostoi.com/hydra.html

I played around with the timer and can make it go really fast if you want, using your code.
Faster even than my method I tried first of drawing it on the canvas in pure javascript.

So makes me think this <div> way of things does have some virtues, even though I think it looks more complicated

p.s. I had to google what a hydralisk was to get a gif!

Last edited by donna1; 12-05-2012 at 10:58 AM..
donna1 is offline   Reply With Quote
Old 12-05-2012, 01:21 PM   PM User | #5
donna1
New Coder

 
Join Date: Nov 2012
Location: london
Posts: 55
Thanks: 5
Thanked 1 Time in 1 Post
donna1 can only hope to improve
Just for fun made mine jump with up key now!
http://donoi.hostoi.com/hydrajump.html
donna1 is offline   Reply With Quote
Old 12-06-2012, 02:59 AM   PM User | #6
TechIlliterate
New to the CF scene

 
Join Date: Nov 2012
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
TechIlliterate is an unknown quantity at this point
That works. Thank you very much!
TechIlliterate is offline   Reply With Quote
Old 12-06-2012, 03:20 AM   PM User | #7
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,244
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
How come both TechIlliterate and donna1 have show code with "hydralisk" images?

(donna1 showed me in PM. But showed me a ".jpg" image.)

WEIRD coincidence? Or have "hydralisks" (whatever they are) suddenly become popular?
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 12-06-2012, 07:21 AM   PM User | #8
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by Old Pedant View Post
How come both TechIlliterate and donna1 have show code with "hydralisk" images?

(donna1 showed me in PM. But showed me a ".jpg" image.)

WEIRD coincidence? Or have "hydralisks" (whatever they are) suddenly become popular?
The hydralisk is a zerg evolution of the slothien. While slothiens were peaceful herbivores, the hydralisk is noted as being one of the most fierce and sadistic of the zerg strains. The average hydralisk in 2499 was 5.4m long and weighed 390 kg. Since hydralisks move relatively slowly over open terrain, it is common for hydralisks to burrow and await their prey to enter killing range before attacking,destroying their enemies in a deadly crossfire. Despite their relatively sluggish nature, hydralisks can climb vertical surfaces[6] and continued evolution of the strain through muscular augmentation has resulted in faster hydralisks.

OK?
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
Old Pedant (12-06-2012)
Old 12-06-2012, 06:55 PM   PM User | #9
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,244
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
LOL! That is truly wonderful, Philip!

And I sure as heck hope you made that up on the spot, because that's what I'm crediting you with.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 12-06-2012, 07:15 PM   PM User | #10
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
Tread carefully..
Attached Thumbnails
Click image for larger version

Name:	hydralisk1.jpg
Views:	17
Size:	5.1 KB
ID:	11763  
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
AndrewGSW is offline   Reply With Quote
Reply

Bookmarks

Tags
arrow, javascript, key, movement, wasd

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:51 PM.


Advertisement
Log in to turn off these ads.