View Full Version : Images change on key pressed

03-06-2009, 01:08 AM
Hi everyone!
I'm trying to make a simple small game in JavaScript. It's a character that runs around climbing and descending on ladders which is in fact a .gif that changes position when I press the arrows. I've already did this and now I need to put in other characters with which the main character should interact. The problem is that the script doesn't insert more than one picture at the same time, so it shows only the last image declared. The script works fine for the characters put in individually as you can see in these two examples http://www.d.a.n.s.home.ro/test/animaniac/animaniac2.html , http://www.d.a.n.s.home.ro/test/animaniac/animaniac.html (where there is no edge press the down arrow to continue). Can anyone please help and tell me how to change more than one image in the same HTML on key-pressed? It's a HTML file that calls upon a .js file. I know the game isn't something grate, it's more for my practice, plus it's just at it's beginnings so I hope I woun't get lots of bad critics :p. Thanks a lot!

03-06-2009, 02:07 AM
where's the script with them both together? post it and I'll take a look.

sounds like you need to make an array to hold the various characters and then do the image swapping for each element in that array.

var chars=new Array()

when you get to the swapping

for (num=0,num<chars.length;num++){(your function to switch image for character) num}

post what you got. Good job so far. You should have no problem. Next you have to make some enemies.

03-06-2009, 07:30 PM
Thanks for the nice thoughts, TinyScript! The script, being a little longer, I'll give you the link to where it is, Firefox will automatically open it as text, IE will ask you to download it: http://www.d.a.n.s.home.ro/test/animaniac/script.js. Here's the HTML file :


<script type="text/javascript" SRC="script.js" ></script>
<link rel="stylesheet" type="text/css" href="style.css"/>

<body onload="frames(event);" onkeypress="frames(event);" id="pageBody">


That's it. Nice and small, only the script is long having lots of IFs, what picture to put in the current coordinates.
One more thing, making an "educated" guess I found out what I was doing wrong: I was getting the content of the HTML page and passing it a photo, thus canceling everything I've put there before, realizing that instead of inserting each photo individually I have to concatenate them (since the insertion is made through a string) and insert all of them through the same line. Now it works just fine and you can see it on the page animaniac.html from my last post. Here's the link to the mistaken script, so you can easier understand the problem and how I've fixed it: http://www.d.a.n.s.home.ro/test/animaniac/script2.js. Thanks for everything! The game will most probably get an update everyday and it will remain on the same page, animaniac.html, if you'll be interested in it. Probably I will ask you for help again :D