PDA

View Full Version : JS Game Help from MARK :)


Mark
04-14-2004, 12:18 AM
Hey Guys/Girls,

I haven't been here in about 3/4 of a year, but I'm back, and really need your help!

As some of you'll might know, I'm studying Software Development, and one of my courses includes web scripting. Right now, I'm finishing up my course in javascript but am kinda stuck (to my suprise) on an assignment.

In the attached files I've included whatever I've reached up to, the actual assign requirements, and a screebshot of what it should look like.

These are some of the problems I'm facing right now. I really hope some JS guru knows how I can fix it:

- I'm not sure why my global variables are not working correctly. For example, I want to set global variables for all the form field values, but when I enter the function, it says its "undefined/no object"

- I did one of the bonus requirement, which says to create the game area in a new window. But in the new window, if I click "stop/close", I'm not sure how to stop the spawning of new windows every interval

- in one of the functions, I have the following code:

//if (timeg == "0")
var win = window.open("", "win", "width=450, height=350, resizable=0, posx= window.clientWidth");
var doc = win.document;

I tried to put it in an if statement (so that the window only spawn when the time is 0), but if I do that, I cannot write to the window (says "object not defined" or something like that)

- lastly, I'm not too sure how to detect what grade letter the user clicked on. I created seperate div tags for each grade, and gave it a name... butI'm not sure how to detect it and add the points.

Someone please help ...

(this is kinda embarrasing for me as I'm one of the top ppl in the class ... lol ... it's just this assign that is screwing me up)

Thanks!

Mark

Mark
04-14-2004, 12:19 AM
Oh, btw, ignore the code

posx= window.clientWidth");

... I was just trying to figure out a way to center the window on the screen. I think I know to fix that.

Willy Duitt
04-14-2004, 01:41 AM
What's the question?

If it is regarding global variables.
I didn't see any in the two files I opened.

eg:
function check() {
var errors = "The following errors occured:";
var quick = document.cntrl.speed.value;
var timeg = document.cntrl.timeelap.value;
var timel = document.cntrl.roundlen.value;



Global variables are defined outside of
any function by using the keyword (var)

eg:
var errors = "The following errors occured:";
function check() {
var quick = document.cntrl.speed.value;
var timeg = document.cntrl.timeelap.value;
var timel = document.cntrl.roundlen.value;


And variables used within a function you either use the keyword var to define a local variable (will die when function is done) or keep it global by omitting the keyword var.

Clear as mudd?
.....Willy

Mark
04-14-2004, 06:03 PM
Hi,

First, thanks for your reply.

Second, sorry about not explaining it properly. I will try again ...

I TRIED to use global js variables, but then decided to use local variables as it wasn't working out. Anyways, in the modifed, re-attached file, I've changed the file (controls.htm) to include the global var. However, I still face 2 problems, which are both mentioned at the spots the errors are occurring.

After these 2 problems are fixed, my third final problem is how do I detect which layer the user clicked so that I can add the individual scores? What I did so far is that when I wrote the grade letters to the new window, I put each one in a div tag, giving it names c1, c2, c3, etc.

That's about it for the main problems.

Any help whatsoever is appreciated.

Thanks,

Mark

n8thegreat
04-14-2004, 10:46 PM
you have to put all the stuff that stores the inputs into a variable in an onload event. since the elements havent been loaded when that javascript is parsed, it cant get the element, so its just undefined or whatever.


window.onload = function() {
//get the inputs
}

Mark
04-14-2004, 11:29 PM
Hi,

After modifying the idea you gave, I've now fixed problem 1. Thanks!

However, I still face troubles with problem 2 (outlined in the attached file), and problem 3 (mentioned in the above post). If somebody can help with this, it would be awesome!

n8thegreat
04-15-2004, 04:27 PM
if you want to fix the window thing, jsut dont get the input value for the timeg when you call load(). instead just set timeg = 0; and then in your if statements just use if (timeg == 0). that worked for me.

*edited this out because of sidescrolling*

Mark
04-15-2004, 08:47 PM
Hi,

Actually before that, I managed to fix all my problems in a other way. Now, I face one final problem which I hope someone solves quickly (assignment is due within a couple of hours).

Attached is all the files I have so far. If you look at the "gamewin.htm" I have 2 places over there where I refer to the left part of the frame from the new window. I use it to get the values from the form fields, and load it into 2 different variables on the page.

In the IE browser, this shows up perfectly, andve EVEYTHING works fine. However, in NN7, it doesn't recognize this. It says "opener.parent.controls.cntrl.speed.value;" has no properties (the same deal is for the other line).

How do I fix this so it works in NN7 and IE?

n8thegreat
04-15-2004, 09:05 PM
did you try window.opener.document.form.stuff.here?

also, just so you know, you can use a with() statement ;)
with (document.getElementById("c"+ i).style) {
top = "100px";
left = "100px";
}

and make sure, when you do the top and left coords, you do numA +"px"; or whatever it is.

Mark
04-15-2004, 09:17 PM
Thanks for the 2nd tip.. I knew that but forgot about it

I don't get what you mean by "window.opener.document.form.stuff.here"

Should I put it in the new window code like this?

var quick = window.opener.document.cntrl.speed.here; // speed of change

n8thegreat
04-15-2004, 09:30 PM
instead of using the opener.parent.controls.cntrl.speed.value to access the box, use window.opener.document.form.stuff.here

Mark
04-15-2004, 09:38 PM
Hi,

It doesn't seem to work.

I used what you gave exactly, and also tried:

window.opener.document.cntrl.speed.here; // speed of change

cntrl - name of form
speed - form field

Mark
04-15-2004, 09:41 PM
Oh, also, I noticed one more thing I forgot to mention.

NN7 doesn't seem to detect and position my layers with this code:


with (document.getElementById("c"+ i).style) {
position='absolute';
left=numA;
top=numB;
color=grcol[i-1];
fontFamily = grfont[i-1];
fontSize = i*10;
}


If you look in the file, it goes through the loop (counter is i), numA/numB is positions, and the other 2 are arrays. It work's perfectly in IE, but crappy netsacpe spoils it.

Help?

n8thegreat
04-15-2004, 09:41 PM
this is the code i used, and it worked:
var quick = window.opener.document.cntrl.speed.value; // speed of change
and
window.opener.document.cntrl.rawscore.value = total;

now, you also have to add the +"px" for the top, left, and fontSize properties in order for it to work in mozilla/ns.
all properties that define size or position have to have their units ;)

Mark
04-15-2004, 09:48 PM
wow, thnx soooo much... everything works now! :D :D :D