How do you vary the x position?

  • Adde
  • Beginner
  • Beginner
  • Adde
  • Posts: 47

Post 3+ Months Ago

I'm a big noob, just wanna test doing a thing(a kinda mini-game) but i need to know one thing... i think of doing a mini-game when you have 3 persons and then you will press a button and the persons will change position with eachother... and then you shall guess who is who (the name of the persons is going to be said in start), However i want to know how to make a button that can make them change the x position
i mean so it wont be the same position of the persons everytime, so they variate... this is maybe kinda hard to understand but please help me... the x position i want it to variate by is 111, 232 and 434.

THANK YOU!
  • Adde
  • Beginner
  • Beginner
  • Adde
  • Posts: 47

Post 3+ Months Ago

ops... forgot to do [/color], sorry
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13504
  • Loc: Florida

Post 3+ Months Ago

Code: [ Select ]
xArray = [111,232,434];
buttonInstanceName.onPress = function(){
   r = random(20);
   for(i=0; i<r; i++){
     xArray.push(xArray[0]);
     xArray.shift();
   }
   person1._x = xArray[0];
   person2._x = xArray[1];
   person3._x = xArray[2];
}
  1. xArray = [111,232,434];
  2. buttonInstanceName.onPress = function(){
  3.    r = random(20);
  4.    for(i=0; i<r; i++){
  5.      xArray.push(xArray[0]);
  6.      xArray.shift();
  7.    }
  8.    person1._x = xArray[0];
  9.    person2._x = xArray[1];
  10.    person3._x = xArray[2];
  11. }


Now, for the line by line :D

xArray = [111,232,434];
^ This is an array that holds the numbers we are going to use as _x positions, one for every movieclip we want to move.

buttonInstanceName.onPress = function(){
^This is the instanceName of the button that you want to trigger this function, onPress is a dynamic event handeler, =function(){ is standard for saying that the next few lines of code are going to be grouped together and run onPress of buttonInstanceName.

r = random(20);
^This is assigning a random number between 0 & 20 to a variable. Soon this number will be how many times we shuffle the array of numbers before using the numbers as _x positions.

for(i=0; i<r; i++){
^This is a for loop, with how we are using it here it's basically saying, "i equals zero to start with, as long as i is less than the random number that was set for r then do everything between { & }, every time the things between { & } have been done the add 1 to i, check that i is still less than r, if it is then do everything between { & } again."

xArray.push(xArray[0]);
^This tells our array to add whatever is at the beginning of the array to the end of the array.

xArray.shift();
^This removes the the item at the begining of the array. (see how the array elements are playing leapfrog for random turns ?)

}
^This ends the for loop.

person1._x = xArray[0];
^This assigns the first number in the array as the _x position of the movieclip with the instanceName person1

person2._x = xArray[1];
person3._x = xArray[2];

^Theese are the same as above just take note of how the number inside [ ] changes.

}
^This ends out function.

Now what do you do with all of this you ask, you place it in the main timeline of your movie, place the movieclips on the stage, either change buttonInstanceName to the instance name of your button or change the instance name of your button to buttonInstanceName.
Change person1(2,3) with the instance names of your person clips or vice versa.
  • Adde
  • Beginner
  • Beginner
  • Adde
  • Posts: 47

Post 3+ Months Ago

Cool, thanks, havent tested it yet but i will in less then 5 seconds :P
  • Adde
  • Beginner
  • Beginner
  • Adde
  • Posts: 47

Post 3+ Months Ago

Didnt get it to work but i still learned something, thanks

Post Information

  • Total Posts in this topic: 5 posts
  • Users browsing this forum: No registered users and 12 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.