Closed Thread Icon

Topic awaiting preservation: Anyone feel like... Pages that link to <a href="https://ozoneasylum.com/backlink?for=8489" title="Pages that link to Topic awaiting preservation: Anyone feel like..." rel="nofollow" >Topic awaiting preservation: Anyone feel like...\

 
Author Thread
Archonian
Nervous Wreck (II) Inmate

From: Sweden, Malmoe
Insane since: Jan 2003

posted posted 01-26-2003 20:33

Hey!
How ya'll doing?

Anyone feel like looking through this code and give me advice on how to optimize it?

I removed the code since I optimized it myself... =)

[This message has been edited by Archonian (edited 02-05-2003).]

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 01-26-2003 21:02

"Anyone feel like looking through this code and give me advice on how to optimize it?"

Heh. That's a lotta code. Not me, thanks =)

"And could anyone tell me the difference between a variable that has been declared implicitly and one that hasn't?"

Scope. If you declare a variable with the "var" keyword, it won't be visible outside of the current set of curly braces. So...

var a=3;
if (blah) {
a=4;
}
alert(a); // says "4"

but...

var a=3;
if (blah) {
var a=4;
}
alert(a); // says "3"

This is incredibly important with recursive functions that involve for loops.

"If you use an if statement like this: ... and statement1 is false does it break after the first statement check or does it continue to check statement2 as well?"

JavaScript, I believe, will break after the first statement.

This is unrelated, but note that the comparison to "true" is unnecessary. This:

if (statement1 == true && statement2 == true) ...

is equivalent to this:

if (statement1 && statement2) ...

Archonian
Nervous Wreck (II) Inmate

From: Sweden, Malmoe
Insane since: Jan 2003

posted posted 01-26-2003 21:13

Hehe =)

Yeah, I thought you'd say that, I'll try to shorten it, cause it's a lot of repeating bits of code actually.

-------------------------------------------------------------------------------

But is
if (statement1 && statement2) ...
faster than
if (statement1 == true && statement2 == true) ...
?
if one overlooks the very bytes that those extra letters will take up.

-------------------------------------------------------------------------------

what is the difference between declaring a variable like this:
var something = 0;
and
something = 0;
if the something variable doesn't exist.

-------------------------------------------------------------------------------

Ok, here the code comes shortened:


var on = 0;
var cmap = 0;
var msgn = 0;
var step = 0;
var frame = 0;
var bstep;
var msgactive = false;
var finished = true;
var wx,wy,wxm,wym;
var tmaps = 2;
var los = 16;
var fdir = "left";
var smrgn = 16;
var ol = 32;
var MAP = new Array(35);
var px = new Array(24,25);
var py = new Array(9,10,11);
var plp = new Array(384,144,32,48);
var noo = new Array(4,2);
var x = new Array(tmaps);
var y = new Array(tmaps);
var xm = new Array(tmaps);
var ym = new Array(tmaps);
var cls = new Array(tmaps);
var st = noo[cmap]-1;
var msg = new Array();
msg[0] = new Array();
msg[0][0] = "Use the Numlock-Keys to navigate...<br>Press the \"1\" key to hide this box!<br>Then navigate using the left, up, right, down arrow keys.<br>Talk & Open Chests with the \"7\" key.";
msg[0][1] = "This is message number one.";
msg[0][2] = "This is message number two.";
msg[0][3] = "You Found Ruby!";

function init() {
for (var i=0; i<35; i++) {
MAP[i] = new Array();
}
for (var i=0; i<tmaps; i++) {
x[i] = new Array(noo[i]);
y[i] = new Array(noo[i]);
xm[i] = new Array(noo[i]);
ym[i] = new Array(noo[i]);
cls[i] = new Array(noo[i]);
}
bstep = 90 + (Math.round(Math.random() * 50));
createMap0();
}

function walkLeft() {
finished = false;
if (frame < 4) {
if (frame == 0) { plr.src = 'newDude/leftR.gif'; plp[0] -= 4; plr.style.left = plp[0]; }
if (frame == 1) { plr.src = 'newDude/leftS.gif'; plp[0] -= 4; plr.style.left = plp[0]; }
if (frame == 2) { plr.src = 'newDude/leftL.gif'; plp[0] -= 4; plr.style.left = plp[0]; }
if (frame == 3) { plr.src = 'newDude/leftS.gif'; plp[0] -= 4; plr.style.left = plp[0]; }
frame++;
setTimeout("walkLeft()",40);
}
else { finished = true; frame = 0; }
}

[This message has been edited by Archonian (edited 02-05-2003).]

Dracusis
Maniac (V) Inmate

From: Brisbane, Australia
Insane since: Apr 2001

posted posted 01-27-2003 09:16

You prolly don't need to use "var = " if the variables are in the main script body (ie, not enclosed inside curly barckets {...}).

So:

var cmap = 0;
var walk = 0;
var objn = 0;
var msgn = 0;
var step = 0;
var frame = 0;


and

var x = new Array(tmaps);
var y = new Array(tmaps);
var xm = new Array(tmaps);
var ym = new Array(tmaps);
var z = new Array(tmaps);
var cls = new Array(tmaps);


Could simply be:

cmap = walk = objn = msgn = step = frame = 0;
x = y = xm = ym = z = cls = new Array(tmaps);



As for:

if (statement1 && statement2) ...

being faster than

if (statement1 == true && statement2 == true) ...

I doubt it. Javascript will automatically try and evaluate anything in an if() to true as that's the most common task an if() statment will perform. Adding the "==" is like asking someone how many fingers your holding up then telling them that they need to actually count your fingers before they'll get the answer. Rather pointless really.

[This message has been edited by Dracusis (edited 01-27-2003).]

Archonian
Nervous Wreck (II) Inmate

From: Sweden, Malmoe
Insane since: Jan 2003

posted posted 01-27-2003 09:43

Could that then be used in the init() function as well when looping through the arrays:

So that:
x[i] = new Array(noo[i]);
y[i] = new Array(noo[i]);
xm[i] = new Array(noo[i]);
ym[i] = new Array(noo[i]);
z[i] = new Array(noo[i]);
cls[i] = new Array(noo[i]);

will be:
x[i] = y[i] = xm[i] = ym[i] = z[i] = cls[i] = new Array(noo[i]);

?

Thanks for the advice.
Would appreciate even more replies.

Clay
Nervous Wreck (II) Inmate

From: Utreg, NL
Insane since: Nov 2002

posted posted 01-27-2003 10:05
code:
cmap = walk = objn = msgn = step = frame = 0;
x = y = xm = ym = z = cls = new Array(tmaps);



that won't do the trick. It will work, but not as intended. An array is an object, and in js, all var's pointing to objects only refer to the object, they don't contain it. So in this case, all the listed vars would point to the same new array, not to individual arrays. A change in xm would also change an index value in ym, cls, and all the other arrays, since they refer to he same array.

copying an array like "arrayA = arrayB" wont work either, it'll just create a new pointer to the same object. Broke my head on this when I first started scripting never understood why my code didn't work they way I wanted.

You could make it shorter by not doint "new Array(length)" but simply "[]", so "var xm = [];" I don't know the use in stating the length anyway, I rather have some global var stating how much entries the array will get somewhere else, and unlike Java, you can make the array longer or shorter at will, so there's no real need to state the length anyway.

peterned

Archonian
Nervous Wreck (II) Inmate

From: Sweden, Malmoe
Insane since: Jan 2003

posted posted 01-27-2003 19:33

Hi!

Is there any way of declaring a two-dimensional array in one line? I know that JavaScript uses arrays in arrays. I mean without looping through the first array's objects and create a new array in each of the items.

Clay
Nervous Wreck (II) Inmate

From: Utreg, NL
Insane since: Nov 2002

posted posted 01-27-2003 19:59

the quickest way I know is:

code:
var thing = [
[1,2,3,4,5,6],
[1,2,3,4,5,6],
[1,2,3,4,5,6],
[1,2,3,4,5,6],
[1,2,3,4,5,6]
]



peterned

Archonian
Nervous Wreck (II) Inmate

From: Sweden, Malmoe
Insane since: Jan 2003

posted posted 01-27-2003 20:15

Thanks,

So, what would this do, exactly? :

var thing = [[]];

What I'm trying to do is to create an empty two-dimensional array.



[This message has been edited by Archonian (edited 01-27-2003).]

Clay
Nervous Wreck (II) Inmate

From: Utreg, NL
Insane since: Nov 2002

posted posted 01-27-2003 22:04

that would create an array with one entry at the first index: another empty array. I'm not much into java a lot, but the 2 dimensional array in that way does not exist in javascirpt, it's just array's inside arrays. So you cant really make an empty 2d array and say thing[20][35] = "value", because thing[20] will have to contain at least an empty array not to cause an error.

peterned

Dracusis
Maniac (V) Inmate

From: Brisbane, Australia
Insane since: Apr 2001

posted posted 01-28-2003 06:11

Oops.. My bad with the array thing. I always thought that would work.

Archonian
Nervous Wreck (II) Inmate

From: Sweden, Malmoe
Insane since: Jan 2003

posted posted 01-28-2003 14:39

Ok, thanks a lot. It's a little bit clearer now.

Well, if anyone feel like taking a look at the code in detail and give me more advice on how to optimize it, go ahead.

Have anyone ever had experience in animations freezing when moving or showing layers. And if so, do you know if there's a way to keep it going. I don't want to reload it, cause that'll be too slow. I heard something about a CANVAS animation. But I don't really know what that is. It would save me a lot of loading time with using an animation that isn't animated with javascript.

BTW, the code is a piece from an RPG-engine that I'm trying to make completely in DHTML. Has anyone done anything similar? Do you know any methods of checking for obsticles, like houses, people, whatever sprite that comes in the way. Right now, I use an optimized loop that compares the player-sprite's x,y positions with the other sprites' positions. All positions are stored in "two-dimensional" arrays. Looking like: array[mapnumber][objectnumber].

If I was looking for speed in IE, would I rather use a TABLE than a DIV?

I'm also have to use a sorrounding tag for the objects i a map like:
<div id="sorroundingmap">
<img src="image">
<img src="image">
</div>
but DIV's tend to slow things down a lot.
It makes it easy when I delete maps with document.getElementById("sorroundingmap").removeNode(true);

Do you know any other tag that I could use instead just that sorrounds the objects, I have no idea if you know what I'm talkig about but well... make your best try in understanding.

[This message has been edited by Archonian (edited 01-28-2003).]

Clay
Nervous Wreck (II) Inmate

From: Utreg, NL
Insane since: Nov 2002

posted posted 01-29-2003 10:21

sounds pretty big, an rpg engine in dhtml

Freezing animations (as far as I know) only occur with animated gifs when the user clicks link (A). The only reason why this would happen I could make up is that usually a link changes the location of the document, and hence keeping up the animation is not required. You can "fix" this by returning false in the link (onclick), but make sure it doesn't return before the intended function is called.

The gamelib over at javascript-games.org features a sprite object style animation (I thought, don't know the lib well). Instead of animated gifs a "sprite" (js object + div) loads an image layed out as a table where the row defines frames of the animation, and the columns define the various animation types (walking left, right, exploding, etc). (or row and column switched, but you get the idea). This is imo actually a pretty neat approach, although I haven't experimented with it myself.

As for checking for obstacles you'll have to code it yourself, or see how other people did it. dhtml just offers you square html elements, and a js environment that enables you to make up any object based environment. I was coding something some time ago, when a designer looked over my shoulder and saw a "gravity" variable in my code. So he asked "wtf! do you have gravity in dhtml??". I told him "Yes. When you code it you do".
I'm working on a 2D game engine now, because I noticed that I keep on coding the same things over and over again when making a game. This time I defined basic sprites (and subclasses), with physics. So now I can give a sprite physics like:

code:
spriteInstance.setPhysics(Physics.SOLID);
// or water, ice, etc.



and the object will behave like the given physics A unit will slide to a halt over ice, and drop and be slowed in water, etc. It's what you code that makes it all happen.

peterned

Archonian
Nervous Wreck (II) Inmate

From: Sweden, Malmoe
Insane since: Jan 2003

posted posted 01-30-2003 00:18

That's exactly what I'm trying to do, a 2D-RPG-engine. I'm a fan of all the old 2D-RPG-styled games.

Now, what I'm curious about with your engine is how you actually move your (sprite(s)?)?. The way I'm doing it now, I'm limited to 16-pixel distances in everything. x,y,width,height variables on all objects most all be equally divisible with 16. The player-sprite moves in 16-pixel steps.

I just got an idea yesterday that will save me a lot of speed. I was writing an algorithm that decided whether the player-sprite object was "behind" or "in front" of another object, (i.e. a tree). To get a little bit of a 3D-feeling to it. And I looked at some old games like, Chrono Trigger, Secret of Mana... Noticed a method that I think they were using. I'm gonna cut all objects tops maybe 32 pixels down and make that slice stand outside of the loop that is executed everytime the player takes a step, and checks whether there is an object in the way or not. It will have a z-index higher than the player-sprite and then the player-sprite will always be behind that object. The other part of the object, the underpart is going to be "looped". That part is always gonna have a z-index lower than the player-sprite. So, it works out. And what it means when I say that it should be looped is that it should be recognised as an object that you cannot walk through.

[This message has been edited by Archonian (edited 02-05-2003).]

Clay
Nervous Wreck (II) Inmate

From: Utreg, NL
Insane since: Nov 2002

posted posted 01-30-2003 10:38

Because I want to able to make both platform style games and arcade style games based on the same game engine I did not limit movement to a set amount of pixels. Sprites can move to any place, provided they don't collide with other sprites that are set to be "solid". When they collide a collision event is triggered, telling whether the sprite collided with a wall, floor or ceiling, so I can react on the event.
With a lot of sprites in the game this could considerably slow down the game, so sprites have a static boolean property, defining whether the sprite is able to move at all. If not, it won't check its collision with other sprites, since it won't ever reach others when it can't move anyway.

It's still all beta though :P It sounds better than what I could currently show.

peterned

Archonian
Nervous Wreck (II) Inmate

From: Sweden, Malmoe
Insane since: Jan 2003

posted posted 01-30-2003 20:54

But how do you make the collision check? It doesn't seem as you make a check with x,y coordinates, do you?

I got curious about scrolling. Cause I hadn't thought of that before and wasn't gonna use it but...
If I create an IFRAME, and the content is bigger than than the iframe can show and I wanted to center the content but I don't want to move the content itself. Is there a way to make the iframe show parts of the content? And exactly how does it work. What should I think about?

The javascript-games.org tutorial explained how to do it, but I didn't completely understand how they did it.

[This message has been edited by Archonian (edited 02-07-2003).]

Diggstar
Obsessive-Compulsive (I) Inmate

From:
Insane since: Feb 2003

posted posted 02-11-2003 14:52

Hey Merlin

Im also a fan of the old school rpg games, i also wanted to make my own rpg game for the snes but never knew how to and couldnt put in the effort. Still needing your help dude.

Drop me a line

Archonian
Nervous Wreck (II) Inmate

From: Sweden, Malmoe
Insane since: Jan 2003

posted posted 02-11-2003 19:30

Hey man. =)

Yeah, but I don't do it in C++, which is exacly I want to and I do learn in the meantime. To deal with graphics, by my experience and what I've heard is quite difficult in C++.

Well, well...

C-Ya

[This message has been edited by Archonian (edited 02-11-2003).]

Diggstar
Obsessive-Compulsive (I) Inmate

From:
Insane since: Feb 2003

posted posted 02-12-2003 00:09

sou des ka?

Ah sou,
Well Merlin, keep in mind that the long time i have known you there has never been anything i saw you couldnt do with a website, and that you always the html master for me dude. But im trying to break my layout barrior to create my own sense of style, but its actually more hard then i fought.

But you know im not one to give up.
But just excuse me while i pass out.
*Yawns* still havent had enough sleep.

« BackwardsOnwards »

Show Forum Drop Down Menu