Topic:   Stumped Over Code   (Read 4313 times)


0 Members and 1 Guest are viewing this topic.

Silverwind


  • ^ This guy is amazing.

  • ****


  • Posts: 2805

  • For the glory of my maker
Stumped Over Code
« on: October 07, 2009, 01:31:33 AM »
Whilst waiting for the tutor to arrive in a web design class last night I wrote this JavaScript routine to make a box bounce around the screen. It didn't work, so I rewrote it when I got back home. But it still didn't work, so I spent an hour puzzling over it to no avail.

By my reckoning there's nothing wrong with it, but evidently DW thinks otherwise. Fame and fortune to whoever spots my mistake:

Code: [Select]
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>My Webpage</title>
<script type = "text/javascript">
<!--
var horzWall = 800;
var vertWall = 800;

function Entity () {
this.x = 50;
this.y = 50;
this.horzDirection$ = "Right";
this.vertDirection$ = "Down";
this.apDiv$ = "apDiv2";
this.Move = Move;
}

function Move () {
if (this.horzDirection$ == "Left") {this.x = this.x - 3;}
if (this.horzDirection$ == "Right") {this.x = this.x + 3;}
if (this.vertDirection$ == "Up") {this.y = this.y - 2;}
if (this.vertDirection$ == "Down") {this.y = this.y - 2;}

if (this.x < 0) {this.horzDirection$ = "Right";}
if (this.x > horzWall) {this.horzDirection$ = "Left";}
if (this.y < 0) {this.vertDirection$ = "Up";}
if (this.y > vertWall) {this.vertDirection$ = "Down";}

element = document.getElementById (this.apDiv$);
element.style.left = this.x;
element.style.top = this.y;
}

function TimedCount () {
pic.Move ();
timer = setTimeout ("TimedCount ()", 100);
}
-->
</script>
</head>
<body>

<div id="apDiv2" style="width:100px; height:100px; background-color:#FFCC00; text-align:center"><p><br /><br /><i>Text</i></p></div>

<script type = "text/javascript">
<!--
pic = new Entity ();
TimedCount ();
-->
</script>

</body>
</html>
I survived the spammage of 2007

GMG Hendo


  • GMG-er

  • **


  • Posts: 155

  • [WITTY TEXT GOES HERE]
Re: Stumped Over Code
« Reply #1 on: October 07, 2009, 10:21:19 PM »
Hm, looks fine here so far. I'm no javascript expert but looking at your basic code structure everything looks correct.

Silverwind


  • ^ This guy is amazing.

  • ****


  • Posts: 2805

  • For the glory of my maker
Re: Stumped Over Code
« Reply #2 on: October 08, 2009, 07:57:57 AM »
That's what I feel. It's only a crappy, unimportant routine, but it's annoying that it won't work. :-/

Hmm... I'm gonna Google a JavaScript forum...
I survived the spammage of 2007

Mystor


  • GMG-er

  • **


  • Posts: 696

  • I am the myst that consumes us all.
Re: Stumped Over Code
« Reply #3 on: October 08, 2009, 08:40:13 AM »
I don't think you're allowed $s in most application's variable names, try to remove those.

Mist

EDIT: I also don't think that you defined pic correctly, objects and functions are very different.

Mist
« Last Edit: October 08, 2009, 08:46:04 AM by mistron »
"I'll lie to him."
"And if that doesn't work?"
"Then I'll tell the truth. We're allowed to do that, in emergencies. We can't plan for everything, you know."
   -Colonel Graff, Ender&

Silverwind


  • ^ This guy is amazing.

  • ****


  • Posts: 2805

  • For the glory of my maker
Re: Stumped Over Code
« Reply #4 on: October 08, 2009, 08:57:25 AM »
Quote
I don't think you're allowed $s in most application's variable names, try to remove those.
Ah. The "$" chars work in Epicus Online, but I'll try removing them just to make sure.

Quote
EDIT: I also don't think that you defined pic correctly, objects and functions are very different.
Oh? What way should I do it?
I survived the spammage of 2007

Silverwind


  • ^ This guy is amazing.

  • ****


  • Posts: 2805

  • For the glory of my maker
Re: Stumped Over Code
« Reply #5 on: October 08, 2009, 05:27:00 PM »
Fixed. I forgot to set the div's position to absolute. (and I also had some direction related code wrong)
I survived the spammage of 2007

Mystor


  • GMG-er

  • **


  • Posts: 696

  • I am the myst that consumes us all.
Re: Stumped Over Code
« Reply #6 on: October 08, 2009, 06:14:53 PM »
Quote
Fixed. I forgot to set the div's position to absolute. (and I also had some direction related code wrong)
Yea, that would do it

I feel ashamed that I did not notice that...

Mist
« Last Edit: October 08, 2009, 06:15:05 PM by mistron »
"I'll lie to him."
"And if that doesn't work?"
"Then I'll tell the truth. We're allowed to do that, in emergencies. We can't plan for everything, you know."
   -Colonel Graff, Ender&

Silverwind


  • ^ This guy is amazing.

  • ****


  • Posts: 2805

  • For the glory of my maker
Re: Stumped Over Code
« Reply #7 on: October 08, 2009, 06:18:42 PM »
So do I; look at the engine I wrote for EO: http://www.gamemakersgarage.com/cgi-bin/yabb/YaBB.cgi?board=news;action=display;num=1249604778;start=0#4

btw, the correct code is:
Code: [Select]
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>My Webpage</title>
<script type = "text/javascript">
<!--
var horzWall = 500;
var vertWall = 300;

function Entity () {
 Â     this.x = 50;
 Â     this.y = 50;
 Â     this.horzDirection$ = "Right";
 Â     this.vertDirection$ = "Down";
 Â     this.apDiv$ = "apDiv2";
 Â     this.Move = Move;
}

function Move () {
 Â     if (this.horzDirection$ == "Left") {this.x = this.x - 2;}
 Â     if (this.horzDirection$ == "Right") {this.x = this.x + 2;}
 Â     if (this.vertDirection$ == "Up") {this.y = this.y - 1;}
 Â     if (this.vertDirection$ == "Down") {this.y = this.y + 1;}
 Â     
 Â     if (this.x < 0) {this.horzDirection$ = "Right";}
 Â     if (this.x > horzWall) {this.horzDirection$ = "Left";}
 Â     if (this.y < 0) {this.vertDirection$ = "Down";}
 Â     if (this.y > vertWall) {this.vertDirection$ = "Up";}
 Â     
 Â     element = document.getElementById (this.apDiv$);
 Â     element.style.left = this.x;
 Â     element.style.top = this.y;
}

function TimedCount () {
 Â     pic.Move ();
 Â     timer = setTimeout ("TimedCount ()", 20);
}
-->
</script>
</head>
<body>
<div id="apDiv1" style="position:absolute; left:50%; margin-left:-280px; top:10px; width:560px; height:400px;">
 Â     <div id="apDiv2" style="position:absolute; width:100px; height:60px; background-color:#FFCC00; text-align:center"><p>Jonny</p></div>
</div>
<script type = "text/javascript">
<!--
pic = new Entity ();
TimedCount ();
-->
</script>

</body>
</html>
« Last Edit: October 08, 2009, 06:21:19 PM by Silverwind »
I survived the spammage of 2007