-1

I don't know how to describe this problem but I will try to be clear enought. I am doing an actionscript exercise where I have a DisplayObjetct Container defined as Sprite, and what I want is to add ramdomizely circles with a random radio length between 50 and 100.

The Container called "gameZoneContainer_sp" has been added to the displaylist in the Main class, and everytime the user change the size of the stage the container set its new values of width and height.

Here is the code of what i am saying:

private function refreshScreen(ev:Event= null ):void{

        // Remember: we can know stage dimensions accessing to:
        // stage.stageWidth
        // stage.stageHeight

        //CONSTANTS DEFINITION OF MARGIN
        const margen:Number=25;

        //LOCAL VARIABLES DEFINITION
        var cercleWidth:Number, cercleHeight:Number;
        var invaderWidth:Number, invaderHeight:Number;
        var containerWidth:Number, containerHeight:Number;
        var logoWidth:Number, logoHeight:Number;

        //STORING DIMENSIONS
        cercleWidth=addCercle_bt.width;
        cercleHeight=addCercle_bt.height;
        invaderWidth=addInvader_bt.width;
        invaderHeight=addInvader_bt.height;
        containerWidth=gameZoneContainer_sp.width;
        containerHeight=gameZoneContainer_sp.height;
        logoWidth=logoUOC_sp.width;
        logoHeight=logoUOC_sp.height;

        //SET HORIZONTAL POSITION OF THE ELEMENTS
        addCercle_bt.x=(stage.stageWidth/2)+margen;
        addInvader_bt.x=(stage.stageWidth/2)-(invaderWidth+margen);
        gameZoneContainer_sp.x=margen;
        logoUOC_sp.x=stage.stageWidth-(logoWidth+margen);

        //SET VERTICAL POSITION OF THE ELEMENTS
        addCercle_bt.y=stage.stageHeight - (cercleHeight+margen);
        addInvader_bt.y=stage.stageHeight-(invaderHeight+margen);
        gameZoneContainer_sp.y=logoHeight+margen*2;
        logoUOC_sp.y=margen;

        //SET DIMENSIONS OF CONTAINER gameZoneContainer_sp
        gameZoneContainer_sp.width=stage.stageWidth-(margen*2);
        gameZoneContainer_sp.height=stage.stageHeight-(margen*4)-invaderHeight-logoHeight;

    }

    /* THIS METHOD ADD A CIRCLE TO THE CONTAINER  gameZoneContainer_sp */
    private function addCercle(ev:MouseEvent):void {
        // Create new instance of Cercle
        // and add it to gameZoneContainer_sp

        //CONSTANT DEFINITIONS
        const minRadio:Number=50, maxRadio:Number=100;

        //LOCAL VARIABLES DEFINITIONS OF RADIO, COLOR AND CIRCLE
        //THE RADIO CHOOSE A RANDOM VALUE BETWEEN 50 AND 100
        var radio:Number=minRadio+(maxRadio-minRadio)*Math.random();
        var color:uint=Math.random() * 0xFFFFFF;
        var cercle:Cercle=new Cercle(color,radio);


        //
        var minPosX:Number=radio;
        var maxPosX:Number=gameZoneContainer_sp.width/2;
        var minPosY:Number=radio;
        var maxPosY:Number=gameZoneContainer_sp.height-(minPosY);


        // SET POSITION OF THE CIRCULE INSIDE THE CONTAINER
        cercle.x= minPosX + (maxPosX-(2*minPosX))*Math.random();
        cercle.y= minPosY + (maxPosY-(2*minPosY))*Math.random();
        cercle.drawCercle();
        gameZoneContainer_sp.addChild(cercle);
        refreshScreen(ev);

        //TRACING RESULTS...
        trace ("Added cercle!");    
        trace ("alto del contenedor: "+gameZoneContainer_sp.height);
        trace ("Posicion x: "+cercle.x);
        trace ("radio: "+radio);    
    }

        //DEFINITION OF THE CIRCLE CLASS
public class Cercle extends Sprite {
        private var _color:uint;
        private var _radio:Number;

        public function Cercle(color:uint,radio:Number){
            super();
            /* THE CLASS ACCEPT HEX VALUES.*/
            _color= color;
            _radio=radio;
            init();
        }

        public function init():void{
            drawCercle ();
        }
        //METHOD TO DRAW A CIRCLE
        public function drawCercle():void{
            var circle:Shape=new Shape();
            circle.graphics.beginFill(_color,1);
            circle.graphics.drawCircle(0,0,_radio);
            addChild(circle);

        }

    }

Here is my problem: When I add a circle it seems to be fine until I change the dimensions of the windows (the scene). Everytime I update the dimensions, the event execute the method refreshScreen from the main class which update the dimensions and positions of all elements in scene, but after that if i add a new circle, it change the container, and that is wrong because what I want is to add the circle IN the container.

Hope is not too long. Please any help would be great! Thank you in advance!!

************ HERE GOES THE SOLUTION ********************

//METHOD TO ADD A CIRCLE TO THE CONTAINER
private function addCercle(ev:MouseEvent):void {
    // Create new instance of Cercle
    // and add it to gameZoneContainer_sp
    const minRadio:Number=50, maxRadio:Number=100;
    var radio:Number=minRadio+(maxRadio-minRadio)*Math.random();
    var color:uint=Math.random() * 0xFFFFFF;

    // CHANGES///////

    // gameZoneContainer_sp IS 400X400 ORIGINALLY AND WE WANT TO PUT AN INSTANCE OF CIRCLE OBJETCT
    // cercle.x WILL PLACE IN minPosX = radio y maxPosy = 400 - radio
    // TAKING INTO ACCOUNT THAT ITS RADIO IS cercle.width/2
    // cercle.x WILL PLACE IN minPosX = cercle.width/2 y maxPosy = 400 - cercle.width/2
    // (SAME FOR  y)

    var cercle:Cercle=new Cercle(color,radio);

    // Cercle scale correction
    //cercle.width= radio/(gameZoneContainer_sp.width/400);
    //cercle.height= radio/(gameZoneContainer_sp.height/400);

    // Cercle positionning
    cercle.x = cercle.width/2 + ((400-cercle.width)*Math.random());
    cercle.y = cercle.height/2 + ((400-cercle.height)*Math.random());

    // PROPORTION CORRECTIONS
    //cercle.width = (2*radio)*gameZoneContainer_sp.width/400;


    //I ADD IT TO THE CONTAINER
    gameZoneContainer_sp.addChild(cercle);



    trace ("Added cercle!");    
    trace ("alto del contenedor: "+gameZoneContainer_sp.height);
    trace ("Posicion x: "+cercle.x);
    trace ("radio: "+radio);



}
myhappycoding
  • 648
  • 7
  • 20
  • Add this below your `addChild(circle);` at the end `trace(circle.parent);` and let me know what it says – Neal Davis Oct 27 '16 at 17:32
  • It says [Object Circle] twice, is normal it appears twice?? – myhappycoding Oct 27 '16 at 20:22
  • also if I do a tracert(cercle.parent) just below gameZoneContainer_sp.addChild(cercle); I get the message [object MovieClip] :/ – myhappycoding Oct 27 '16 at 20:36
  • object circle or object cercle? Seems like it should be cercle since that is the class name of the object you're doing addChild in. – Neal Davis Oct 27 '16 at 20:46
  • At any rate it seems like the parent child relationship is fine. Can you clarify what is happening wrong? You say that the new circles don't get added to the correct container after you scale things? How do you know that? What are you actually seeing that makes you think that is what is happening? More words please :) – Neal Davis Oct 27 '16 at 20:49
  • ok, I have a container represented graphicilly like a rectangle in the scene. On the other hand I want to draw circles INSIDE the rectangle (container), so it works fine if I dont resize the window, but when I resize the window (scene) it stops working because the circles are added to the scene (at least graphically). Is there anyway to explain you this with a picture?? It would be easier ;) – myhappycoding Oct 27 '16 at 21:07
  • You can upload a picture or a link to a picture. – Neal Davis Oct 27 '16 at 21:08
  • You say that graphically it isn't added to the container. How so? What do you see when you try to add new circles after resizing the window? The circles all line up in the corner? They don't appear at all? They appear but they aren't resized like the container? That's the question I'm trying to get at. Sorry if I'm not being clear – Neal Davis Oct 27 '16 at 21:10
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/126870/discussion-between-galeonweb-and-neal-davis). – myhappycoding Oct 27 '16 at 21:10
  • I think your cercle.x math is a little odd looking. I would try `cercle.x = Math.random()*gameZoneContainer.width;` no need to add more X for the min value since it's being added to the container it will have a local X min that matches the global X of the container it is in. – Neal Davis Oct 27 '16 at 22:15
  • did my answer solve your problem? – Neal Davis Nov 01 '16 at 16:37
  • no, sorry for my delay!!, your solution is not valid. All I was doing was right because the way I was adding the circles was taking into account the margin and the circle's radius for not to exceed the bounds of the container. So what I didn't know is that the container was originally defined with a height and a width of 400px and I dont know why but even changing the dimensions everytime I update the screen the dimensions are allways 400x400px. – myhappycoding Nov 02 '16 at 18:19
  • Obviously changing the size of the display window won't change the dimensions of a Sprite (your container). But you say you are unable to change the dimensions of the container at runtime? That should be pretty trivial right? Can you add the code you are using to change the dimensions of the container? – Neal Davis Nov 02 '16 at 20:31
  • Because you know that just changing the dimensions of the container won't update the values of maxPos, right? – Neal Davis Nov 02 '16 at 20:32
  • how or where can I add the code?, can I open a new answer for that?. what I do is this: gameZoneContainer_sp.width=stage.stageWidth-(margen*2); gameZoneContainer_sp.height=stage.stageHeight-(margen*4)-invaderHeight-logoHeight; and that code is inside a function refreshScreen(ev:Event= null ):void which is executed everytime I change the size of my stage window – myhappycoding Nov 03 '16 at 11:47
  • but my teacher told me that to make it work I have to consider that initially the object is 400px of dimensions, and what I was considering that its dimensions were updated to the values I set up in the code :/ – myhappycoding Nov 03 '16 at 11:52
  • Click the edit button below your original post. – Neal Davis Nov 03 '16 at 12:33
  • I think that what this is showing us is that changing the window doesn't change the stage. – Neal Davis Nov 03 '16 at 14:50
  • Can you try stage.width += 50, and does that change the width of the stage? – Neal Davis Nov 03 '16 at 14:51
  • If I do that, it gives me the error: "The Stage class does not implement this property or method" :/ – myhappycoding Nov 05 '16 at 08:23
  • I have updated my post with the solution ;). Thank you very much for your interest. – myhappycoding Nov 05 '16 at 08:33

1 Answers1

0

This is what I think is going on. You basically have 2 problems.

  1. You're adding circles using math meant to adjust for the position of the game window, but you don't need that if you are adding it to the game window container. This is resulting in circles that are being added too far to the right.
  2. Something about your Air setup is making the screen contents scale to show all the contents resulting in everything shrinking. (If we fix the first problem, this one will become inconsequential).

So instead of:

cercle.x = minPosX ...etc

do:

cercle.x = Math.Random()*gameZoneContainer_sp.width;

Do likewise for the y value.

You see, the local origin point of an objects container will be that objects (0,0) point locally. So the top left corner of this game container will be the cercle's (0,0) point and therefore there is no need to adjust it to the right by the amount that you do using minPosX. This is assuming you have not made your game container offset from the default somehow (i.e. When you created the rectangle, you did so using (0,0,width,height) and not something like (xOffset,yOffset,width,height)).

Neal Davis
  • 2,010
  • 2
  • 12
  • 25