mercredi 25 janvier 2017

I have performance issues with the code I'm using to detect collision between items

I managed to make this work and do what I needed after a while but now my problem is performance. As you can see I need it for 14+ items. I tested at first for 5 items and it worked ok but after I added more the performance dropped a lot. As for the collision detection class, I assume you know what it does and how it works(that I managed to make it work since the registration point only works when set to upper left side of the movie clip and mine was in the middle) If you need any other details or information please don't hesitate to ask and I will try my best to provide it. So in a few words what is need is: -

  • I have 14+ objects and need to display them
  • I need a unique random position for every object
  • Make new random position and check to see if it's shape collides with the other objects
  • If that object doesn't collide with the other object displayed the movieClip
  • If that object collides with another movieClip get another random position
  • Check again the object with the other objects to see if it collides
  • If YES find another position / If NO show the object
  • Do this until all the objects are positioned without colliding with each other

I believe I'm missing something and that this code could be better but really don't have any other good ideas and didn't find anything on the internet that might help. Please give me a hand.

*** Also keep in mind that I don't want the grid solution for collision detection


private var objArray : Array = new Array (movieClip1, movieClip2, movieClip3, movieClip4, movieClip5, movieClip6, movieClip7, movieClip8, movieClip9, movieClip10, movieClip11, movieClip12, movieClip13, movieClip14);
private function init():void
    {
        trace("[TRACE]:Init GameScreen");
        addEvents();
        startTimerCollision();
    }
private function addEvents():void
    {
        _timer.addEventListener(TimerEvent.TIMER, timer); //Listen for the Timer event _timer and trigger the function timeLeft.
        Repository.STAGE_REF.addEventListener(Event.ENTER_FRAME, frameRate);
    }
private function startTimerCollision():void
    {           
        _timer.start();                         //start the timer

        _collisionCheck = new CollisionTest();
    }
private function frameRate(event:Event):void
    {
        //add 1 to the frame count every frame
        _frame++;
    }
function timer(event:TimerEvent):void
    {   
        timpul--
        _TimerSound.play();

        showObjs();
    }
private function randomPosition(obj:MovieClip):void
    {
        obj.x = obj.width + Math.random() * (stage.stageWidth - obj.width * 2);
        obj.y = obj.height + Math.random() * (stage.stageHeight - obj.height * 2);  
    //  trace(obj + " RANDOM POS= " + "x: " + obj.x + " / y:" + obj.y);
    } 

    private function randomALLPositions():void
    {
        for (var i = 0; i < objArray.length; i++)
        {
            randomPosition(objArray[i]);
        }
    }

    private function checkAllCollision(obj:MovieClip):Boolean
    {
        var collisionStatus : Boolean;
        for (var i = 0; i < objArray.length; i++)
        {
            if (obj != objArray[i])
            {               
                if (_collisionCheck.complex(obj, objArray[i]))
                {
                    collisionStatus = true;
//                  trace("collision detected: " + obj + " and " + objArray[i]);
                    randomPosition(obj);
                    checkAllCollision(obj);
                }
                else
                {
                    collisionStatus = false;
                }
            }
        }
        return collisionStatus;
    }

    private function showObjs():void
    {

        ////////////////////////////////////////////////////////ONLINE
        var i:int = 0;
        var j:int;
        var l:int = objArray.length;

        for (i; i < l; i++)
        {
            randomPosition(objArray[i]); 
            for (j = 0; j < l; j++)
            {
                if (objArray[i] != objArray[j])
                {
    //              trace("OBJ " + (i+1) + " with OBJ " + (j+1) + "---------> CHECKING");
                    //while (checkAllCollision(objArray[i]))
                    if(checkAllCollision(objArray[i]) == true)
                    {
    //                  trace("OBJ " + (i+1) + " with OBJ " + (j+1) + "---------> Collision DETECTED");
                        randomPosition(objArray[i]);
    //                  trace("OBJ "+ (i+1) + " NOW= " + "x: " + objArray[i].x + " / y:" + objArray[i].y);
                    }
    //              trace("OBJ " + (i+1) + " with OBJ " + (j+1) + "---------> OK");
                }
            }
            addChild(objArray[i]);

        }
        ////////////////////////////////////////////////////////ONLINE

        for (var z = 0; z < objArray.length; z++)
        {
            TweenLite.to(objArray[z]["gime" + (z+1)], /*time:*/ 0, {scaleX:0, scaleY: 0});
            TweenLite.to(objArray[z]["gime" + (z+1)], /*time:*/ 0.5, {scaleX:1, scaleY: 1});
        }   
    }

Aucun commentaire:

Enregistrer un commentaire