Jump to content

  • Log In with Google      Sign In   
  • Create Account


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.

  • You cannot reply to this topic
5 replies to this topic

#1 nekoluffy   Members   -  Reputation: 124

Like
0Likes
Like

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. 

Attached Thumbnails

  • Capture.PNG


Sponsor:

#2 d4n1   Members   -  Reputation: 423

Like
1Likes
Like

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.


#3 nekoluffy   Members   -  Reputation: 124

Like
1Likes
Like

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.



#4 d4n1   Members   -  Reputation: 423

Like
0Likes
Like

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.

#5 Dezachu   Members   -  Reputation: 370

Like
0Likes
Like

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!


Studying BSc Computer Games Programming @ De Montfort University, Leicester.

Completed a placement as a Junior Programmer at Exient Ltd in Oxford/Valletta, Malta


#6 jbadams   Senior Staff   -  Reputation: 17753

Like
3Likes
Like

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.)

 

smile.png






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.



PARTNERS