View more

View more

View more

Image of the Day Submit

IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

increment & loop function in this code (SOLVED, ignore topic)

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

5 replies to this topic

#1nekoluffy  Members

Posted 27 January 2014 - 10:30 PM

function EnemySearch ()
{
for (i = 0; i < 5; i++) // what does this do? the rest of the code (seems to me like it) is based off the enemyDistance = Random.Range (1, 10)
enemyDistance = Random.Range (1, 10);

if (enemyDistance >= 8) // like this for example. makes use of the number provided from random.range.

#2v1Xus  Members

Posted 27 January 2014 - 10:38 PM

Its doing whatever your code is meant to do, 5 times.  There's nothing more to answer.

I wont ask why you are making up random numbers, it doesnt look like your function is of much use to anything other than perhaps a partially finished unit test.

Edited by d4n1, 27 January 2014 - 10:39 PM.

#3nekoluffy  Members

Posted 27 January 2014 - 10:41 PM

Its doing whatever your code is meant to do, 5 times.  There's nothing more to answer.

I wont ask why you are making up random numbers, it doesnt look like your function is of much use to anything other than perhaps a partially finished unit test.

just learning the basics of java scripting in unity via tutorials. Its not meant to be anything yet. Just trying to understand terms and ways to use them.

#4v1Xus  Members

Posted 27 January 2014 - 11:58 PM

In that case I would recommend going over for loops. If you enjoy JavaScript as much as I do, visit the MDN regularly and review the documentation in there for JS. Good luck.

#5Dezachu  Members

Posted 28 January 2014 - 03:29 AM

As d4n1 says, the for loop will do whatever you specify it to 5 times.

In this case, you'll get 5 printed statements, such as:

The enemy is getting closer!

The enemy is getting closer!

The enemy has terrible breath!

The enemy is way over there!

The enemy has terrible breath!

If you were to remove the for loop, what you're doing is the same as calling that function (EnemySearch) 5x in your main function. Just makes your code considerably smaller you see ;)

As a test of your understanding, why not try doing the same with a 'while' loop? I'll let you figure out what condition you need in the while loop!

BSc Computer Games Programming (De Montfort University) Graduate

Worked on Angry Birds Go! at Exient Ltd

Co-founder of Stormburst Studios

Posted 29 January 2014 - 02:03 AM

As a side-note for future reference, Java and JavaScript are very different languages with no relation to each other, and confusing one with the other when asking for help is likely to result in confusing or incorrect answers.

Unless you're in the unusual position of using both languages on your current project, you don't want to tag your forum topics with the 'Java' tag when you're asking about 'JavaScript' (or vice versa).  (Note: I've fixed the tags on this topic so that it longer has this problem.)