Go Back   kirupaForum > Flash > ActionScript 1.0/2.0

Reply
 
Thread Tools Display Modes
Old 11-22-2009, 08:30 PM   #1
brian_nathaniel
n00b freelancer
 
brian_nathaniel's Avatar
Location Grayslake, IL

Posts 8
Set _X and _Y value from Array

Hello,

Here is what I am trying to do:

I have four circles in four different positions. On top of each circle I have a randomly generated number.

What I would like is to position the numbers randomly on top of the four circles (each above its own circle).

While I can generally read and understand AS, I am still very new to it, and I don't know the proper method for achieving this.

I thought possibly getting the values for the _X and _Y of each number (the numbers are stored in a dynamic text field) from an array.

I was able to find a script which retrieves a random value from an array:

ActionScript Code:
// START - Random value from array //
Array.prototype.randySelect = function(vSelect) 
{ 
    var temp = this.slice(0)
    var result = new Array()
    var i = ((vSelect==null||vSelect>this.length) ? this.length : Math.round(vSelect))
    var x; 
    while(i--) 
    { 
        x = random(temp.length)
        result[i] = temp[x]
        temp.splice(x,1)
    } 
    return result; 
}
position_x = new Array("446.95","572.95");
position_y = new Array("107.2","236.9");

position_x_final = position_x.randySelect(1);
position_y_final = position_y.randySelect(1);
// END - Random value from array //

//This seems to be the problem area//
the_right_answer._x = position_x_final;

trace(position_x_final);
trace(position_y_final);
trace(the_right_answer._x);

Here is what happens when I test my movie:

- trace(position_x_final) returns either "446.95" or "572.95" (without the quotes)

- trace(position_y_final) returns "107.2" or "236.9" (again, without the quotes)

- trace(the_right_answer._x) returns the current position of the number (this is the value that I want to match position_x_final)

- When I change "the_right_answer._x = position_x_final;" to something like "the_right_answer._x = 15;" the object is positioned correctly (_x=15)

Please let me know what I need to change to make this code work, or if there is a better solution to accomplish what I am trying to do.

Thanks!
brian_nathaniel is offline   Reply With Quote

Sponsored Links (Guests Only) - Register | Need Help?
 

Old 11-23-2009, 11:29 AM   #2
FizixMan
Registered User
 
FizixMan's Avatar
Try the_right_answer._x = Number(position_x_final);
FizixMan is offline   Reply With Quote
Old 11-23-2009, 12:08 PM   #3
glosrfc
Registered User
 
glosrfc's Avatar
Location Halley Research Station, Latitude 75°35' S, Longitude 26°39' W, Brunt Ice Shelf, Coats Land, Antarctica

Posts 4,158
I'm not sure why you would need that function. You should be able to just loop through your circle instances, retrieve their _x/_y properties, and then apply the same properties to your randomised number instances. The only issue you have to account for is that a dynamically created textfield has its registration point set to the upper left. So, to centre it onto your circles, you need to set the desired font size and centre align as a textFormat, apply some text, autosize the textfield, and then apply the textFormat. Finally, you retrieve the _height and _width properties of the nicely formatted and sized textfield, and apply half of each value to the _x and _y properties respectively. This demonstrates how to go about it:
ActionScript Code:
for (i = 0; i < 4; i++) {
    var xPos:Number = this["circle" + i]._x;
    var yPos:Number = this["circle" + i]._y;
    this.createTextField("number" + i, this.getNextHighestDepth(), xPos, yPos, 50, 50);
    var my_fmt:TextFormat = new TextFormat();
    my_fmt.align = "centre";
    my_fmt.size = 20;
    this["number" + i].text = Math.round(Math.random() * 20);
    this["number" + i].autoSize = true;
    this["number" + i].setTextFormat(my_fmt);
    this["number" + i]._x = this["number" + i]._x - this["number" + i]._width / 2;
    this["number" + i]._y = this["number" + i]._y - this["number" + i]._height / 2;
}

Edit: To test the example, create a movieclip containing a circle, then place four instances of it onto the stage and name them sequentially from circle0 to circle3.

__________________
©2006 GlosRFC - Searching 8,168,684,336 brain cells

Last edited by glosrfc; 11-23-2009 at 12:11 PM..
glosrfc is offline   Reply With Quote
Old 11-23-2009, 05:06 PM   #4
brian_nathaniel
n00b freelancer
 
brian_nathaniel's Avatar
Location Grayslake, IL

Posts 8
@FizixMan

Thanks for the suggestion. I tried that as well as getAsNumber (mostly just hoping it would work). Unfortunately, no effect.

@GlosRFC

I looked at your code and tested the movie as you suggested. The script works great for generating random numbers in each circle!

I should have been more thorough in my description of what I am trying to accomplish, though.

What I am using this for is to design a simple e-learning game (mostly just to see what I can do with it).

Just for practice purposes, I have a balloon on the left side of the stage. Over the balloon I have a randomly generated number between 1 and 50. To the right of the stage I have four circles (buttons), three of which are also generating a random number between 1 and 50, and one of which has the same number as the balloon (making this the "right answer").

I wrote some code using variables to make sure that only 1 of the circles matches the right answer.

As of right now, when I test my movie I get most of what I am going for: three randomly generated numbers not equal the "right answer", and one number that is.

The problem I am having is that as of right now, each number is always above the same circle. This is fine for the randomly generated numbers, but the "right answer" obviously cannot always be in the same spot.

I tried to see how I could tweak your code to make this work, but it's a bit too advanced for me at this point.

Here is the all of the AS that I am currently using (including the original code that I posted):

ActionScript Code:
import mx.data.binding.TypedValue;
rightAnswer = Math.floor(Math.random() * (47 - 1)) + 1;
wrongAnswer1 = Math.floor(Math.random() * (47 - 1)) + 1;
wrongAnswer2 = Math.floor(Math.random() * (47 - 1)) + 1;
wrongAnswer3 = Math.floor(Math.random() * (47 - 1)) + 1;

onLoad = function() {
    if(wrongAnswer1 == rightAnswer) {
        wrongAnswer1 = wrongAnswer1 + 1;
    }
        if(wrongAnswer2 == rightAnswer) {
        wrongAnswer2 = wrongAnswer2 + 1;
    }
        if(wrongAnswer3 == rightAnswer) {
        wrongAnswer3 = wrongAnswer3 + 1;
    }
};

// START - Random value from array //
Array.prototype.randySelect = function(vSelect) 
{ 
    var temp = this.slice(0)
    var result = new Array()
    var i = ((vSelect==null||vSelect>this.length) ? this.length : Math.round(vSelect))
    var x; 
    while(i--) 
    { 
        x = random(temp.length)
        result[i] = temp[x]
        temp.splice(x,1)
    } 
    return result; 
}
position_x = new Array("446.95","572.95");
position_y = new Array("107.2","236.9");

position_x_final = position_x.randySelect(1);
position_y_final = position_y.randySelect(1);
// END - Random value from array //

the_right_answer._x = Number(position_x_final);

trace(position_x_final);
trace(position_y_final);
trace(the_right_answer._x);


Is there a way to tweak your code to get the effect that I am looking for, or is there a different method that you would suggest?

Thank you for your help!

__________________
Most Recent Project: "Weeds" Quote Generator
http://www.whitetrinity.com/Weeds_Quote_Generator

Nancy Botwin: "I'm the suburban baroness of bud, Nancy."
brian_nathaniel is offline   Reply With Quote
Old 11-23-2009, 06:34 PM   #5
glosrfc
Registered User
 
glosrfc's Avatar
Location Halley Research Station, Latitude 75°35' S, Longitude 26°39' W, Brunt Ice Shelf, Coats Land, Antarctica

Posts 4,158
Try the following technique. First add your random numbers to an array. The first position in that array represents the right answer which you can then store to a variable, e.g. rightAnswer. Then shuffle the array. Finally, use the shuffled array to place the numbers onto the circles. This will ensure that the position of the right answer is a different circle:
ActionScript Code:
var numberArray:Array = new Array();
for (i = 0; i < 4; i++) {
    numberArray.push(Math.floor(Math.random() * (47 - 1)) + 1);
}
var rightAnswer:Number = numberArray[0];
trace(rightAnswer);
numberArray.sort(function () {
    return Math.round(Math.random());
});
for (i = 0; i < 4; i++) {
    var xPos:Number = this["circle" + i]._x;
    var yPos:Number = this["circle" + i]._y;
    this.createTextField("number" + i, this.getNextHighestDepth(), xPos, yPos, 50, 50);
    var my_fmt:TextFormat = new TextFormat();
    my_fmt.align = "centre";
    my_fmt.size = 20;
    this["number" + i].text = numberArray[i];
    this["number" + i].autoSize = true;
    this["number" + i].setTextFormat(my_fmt);
    this["number" + i]._x = this["number" + i]._x - this["number" + i]._width / 2;
    this["number" + i]._y = this["number" + i]._y - this["number" + i]._height / 2;
}

__________________
©2006 GlosRFC - Searching 8,168,684,336 brain cells
glosrfc is offline   Reply With Quote
Old 11-24-2009, 03:08 AM   #6
brian_nathaniel
n00b freelancer
 
brian_nathaniel's Avatar
Location Grayslake, IL

Posts 8
Thank you! The script worked perfectly! Much appreciated =D

__________________
Most Recent Project: "Weeds" Quote Generator
http://www.whitetrinity.com/Weeds_Quote_Generator

Nancy Botwin: "I'm the suburban baroness of bud, Nancy."
brian_nathaniel is offline   Reply With Quote
Old 11-24-2009, 10:38 AM   #7
glosrfc
Registered User
 
glosrfc's Avatar
Location Halley Research Station, Latitude 75°35' S, Longitude 26°39' W, Brunt Ice Shelf, Coats Land, Antarctica

Posts 4,158
No problem. It's usually a lot easier to sequentially read a shuffled array than it is to try to read an unshuffled array in a random manner.

__________________
©2006 GlosRFC - Searching 8,168,684,336 brain cells
glosrfc is offline   Reply With Quote
Reply

Tags
arrays, random positioning, values, variables


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 04:19 PM.

SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple. flash components
Creative web apps. Make your own free flash banners and photo slideshows.
Check out the great, high-quality flash extensions. Buy or sell stock flash, video, audio and fonts for as little as 50 cents at FlashDen.

Flash Transition Effects

Flash Effect Tutorials

Digicrafts Components
Flash effects. Art without coding. Upload, publish, deliver. Secure hosting for your professional or academic video, presentations & more. Screencast.com
Streamsolutions Content Delivery Networks Flipping Book - page flip flash component.
Flash-Gallery.com - Get your flash photo gallery (flash component or swf gallery Learn how to advertise on kirupa.com
 

cdn
content delivery network (cdn)

Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd. Copyright 2010 - kirupa.com Copyright 2010 - kirupa.com