Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

1 decade ago by faytmye

Hi all,

I'm a beginner to ImpactJS and i've been experimenting with the engine for a bit now but I'm running into troubles with something that should be simple.

Basically I just want to print an array to a new line for each entry that exists within in. I just can't understand why it's not working but I'm sure it's something silly that I missed or forgot about.

This is the error that I am getting:
/><br />
<br />
I have two files for the test demo, hero.js and main.js<br />
<br />
hero.js<br />
<pre>
 	hOne: [
	fName="Bruce",
	lName="Wayne",
	title="The Dark Knight",
	hp=100,
	mp=50,
	],
</pre><br />
main.js<br />
<pre>
for (  var i in hero.hOne) {
	this.font.draw( hero.hOne[i] + "\n", 20, 20);
}
</pre><br />
I've also tried tests using hOne.splice() and also tried the below instead:<br />
<pre>
for (var i = 0; i < hero.hOne.length; i++)
</pre><br />
If anyone could shine some light on this I would very much appreciate it. It's been wrecking my head and stopping me from progressing.			</div>
		</div>
			<div class=

1 decade ago by Logan

I noticed that in your array

 	hOne: [
	fName="Bruce",
	lName="Wayne",
	title="The Dark Knight",
	hp=100,
	mp=50              //The comma behind 50 should be removed.
	],

try that? See what happens?

1 decade ago by tuttlegames

I'm not a JS expert but it seems like you're drawing a string and then a line break at the same position, over and over. Naturally, they will overlap and the line breaks will be essentially useless.

I'd do something like:

for (i=0 ; i < hero.hOne.length ; i++) {
    this.font.draw(hOne[i], 20, 20*i);
}

Maybe you can also do something like consolidating all the values you want into one big string, delimited by a "\n", and draw just that one string once.

1 decade ago by faytmye

This fixed the problem, thanks so much :)
20*i

1 decade ago by drhayes

tuttlegames is right, that'll work much better than iterating through the array.

On that note: your syntax is wrong. You don't want an array, you want an object:

hOne: {
  fName: 'Bruce',
  lName: 'Wayne',
 // etc..
}

You can iterate through those properties using a loop like you have, but that output that looks like JS code likely is JS code from the object&039;s prototype. That's because an expression like #hOne['whatever'] will pull not just fields, like you've defined, but also methods on the object or its prototype (kinda like its parent, but just roll with me here).

Change your for loop to look like this, maybe:

var i =1;
for (var j in hero.hOne) {
  if (hero.hOne.hasOwnProperty(j)) {
    ig.font.draw(hOne[j], 20, 20 * i);
    i++;
  }
}

That'll prevent code from showing up like you have in the screenshot.
Page 1 of 1
« first « previous next › last »