A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: x, y integers

  1. #1
    Senior Member
    Join Date
    Mar 2006
    Posts
    139

    x, y integers

    Hello,

    I have a feeling the answer to this is probably 'no' but worth a shot...
    using actionscript I want to round up the xy position of everything on my stage to the nearest integer,
    i.e. if something has a position of x = 24.8, y = 35.7 they would be repositioned to x = 25, y = 36.
    Is this possible?

    thanks!

  2. #2
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    this might help you out.

    PHP Code:
    // *** round up
    myObject._x Math.round(myObject._x);// makes 33.6 > 34
    myObject._y Math.round(myObject._y);// makes 29.6 > 30
    // *** round down
    //myObject._x = Math.floor(myObject._x);// makes 33.6 > 33
    //myObject._y = Math.floor(myObject._y);// makes 29.6 > 29 
    or you could just make sure they have rounded xy coords when you put them on the stage.

    the code below will list all movieclips and if they have x or y coords with decimals in them it will round them up.

    PHP Code:
    for (var i in this)
    {
        if (
    typeof (this[i]) == "movieclip")
        {
            
    trace("movie clip '" this[i]._name "' is current x " this[i]._x);
            
    trace("movie clip '" this[i]._name "' is current y " this[i]._y);
            if (
    this[i]._x.indexOf(".") != -1)
            {

                
    this[i]._x Math.round(this[i]._x);
                
    trace("movie clip '" this[i]._name "' is new x " this[i]._x);
            }
            if (
    this[i]._y.indexOf(".") != -1)
            {

                
    this[i]._y Math.round(this[i]._y);
                
    trace("movie clip '" this[i]._name "' is new y " this[i]._y);
            }
        }

    Last edited by fruitbeard; 10-23-2012 at 12:09 PM.

  3. #3
    Senior Member
    Join Date
    Mar 2006
    Posts
    139
    Thanks,

    but i'm looking for a way to apply the rounding to everything that has xy coordinates, including points in splines as well as MCs and graphics
    without having to reference everything individually.

  4. #4
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    my post edit will surely help you along the way then

  5. #5
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi again,

    this will do it but im not sure about nested clips ect ect yet.

    put it on your main time line

    PHP Code:
    for (var i in this)
    {
        if (
    typeof (this[i]) == "movieclip")
        {
            
    trace("movie clip '" this[i]._name "' is current x " this[i]._x);
            
    trace("movie clip '" this[i]._name "' is current y " this[i]._y);
            if (
    this[i]._x.indexOf(".") != -1)
            {

                
    this[i]._x Math.round(this[i]._x);
                
    trace("movie clip '" this[i]._name "' is new x " this[i]._x);
            }
            if (
    this[i]._y.indexOf(".") != -1)
            {

                
    this[i]._y Math.round(this[i]._y);
                
    trace("movie clip '" this[i]._name "' is new y " this[i]._y);
            }
        }
        if (
    typeof (this[i]) == "object")
        {
            
    trace("object '" this[i]._name "' is current x " this[i]._x);
            
    trace("object '" this[i]._name "' is current y " this[i]._y);
            if (
    this[i]._x.indexOf(".") != -1)
            {

                
    this[i]._x Math.round(this[i]._x);
                
    trace("object '" this[i]._name "' is new x " this[i]._x);
            }
            if (
    this[i]._y.indexOf(".") != -1)
            {

                
    this[i]._y Math.round(this[i]._y);
                
    trace("object '" this[i]._name "' is new y " this[i]._y);
            }
        }


  6. #6
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    This one is less messy.

    PHP Code:
    for (var i in this)
    {
        
    // *** movieclips
        
    if (typeof (this[i]) == "movieclip")
        {
            
    // *** x 
            
    this[i].oldX this[i]._x;
            
    this[i]._x Math.ceil(this[i]._x);
            
    trace("movie clip : '" this[i]._name "' original x is (" this[i].oldX ") new x is (" this[i]._x ")");
            
    // *** y
            
    this[i].oldY this[i]._y;
            
    this[i]._y Math.ceil(this[i]._y);
            
    trace("movie clip : '" this[i]._name "' original y is (" this[i].oldY ") new y is (" this[i]._y ")\n");
        }
        
    // *** buttons, graphics, textfields ...   
        
    if (typeof (this[i]) == "object")
        {
            
    // *** x
            
    this[i].oldX this[i]._x;
            
    this[i]._x Math.ceil(this[i]._x);
            
    trace("Object : '" this[i]._name "' original x is (" this[i].oldX ") new x is (" this[i]._x ")");
            
    // *** y
            
    this[i].oldY this[i]._y;
            
    this[i]._y Math.ceil(this[i]._y);
            
    trace("Object : '" this[i]._name "' original y is (" this[i].oldY ") new y is (" this[i]._y ")\n");
        }


  7. #7
    Senior Member
    Join Date
    Mar 2006
    Posts
    139
    hey, thanks a lot!

    will give it a try later

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center