A Flash Developer Resource Site

Results 1 to 1 of 1

Thread: Event Listeners for many objects stored in array.

  1. #1
    Junior Member
    Join Date
    Nov 2008
    Posts
    6

    Event Listeners for many objects stored in array.

    I'm creating a simple animation program in as3 for fun and learning. The program is getting to a point in complexity that requires me to step in to a bigger view of the programming world. If any of you could give me a hint or advice to point me in the right direction I would be sincerely greatful.

    The program right now places a blue ball on the screen which the player can drag and drop using the mouse. At each drop of the blue ball a point is recorded into points array which is used to draw lines from the last point to the new one. The same point is recorded in coordDots array which is used to draw small red points at each point in points array.

    I'm creating the red balls as items in coordDots array because I want them to have unique names so they can be moved individually. This is my red ball creation code which is run each time the user drops the blue ball thus creating a new point.
    Code:
    var coordDots:Array = new Array();
    //Create new coord Point apon drop.
    function newCoordDot():void {
    	count = points.length;
    	
    	var dot:Sprite = new Sprite();
    	
    	coordDots.push(dot);
    	//board.addChild(dot);
    	dot.graphics.lineStyle(0,1);
    	dot.graphics.beginFill(0xF71616, 1);
    	dot.graphics.drawCircle(0,0,6);
    	dot.graphics.endFill();
    	dot.x = points[count-1][0];
    	dot.y = points[count-1][1];
    	board.addChild(coordDots[count-1]);
    }
    As you can see, the first red ball will be named coordDots[0], second red ball would be coordDots[1], this way I can make as many as I want dynamically without needing to know how many I will need to make before the program begins.

    My question is in regards to my goal of making all of these red dots movable the way the blue ball is. The user should be able to grab a red ball and move it and have the lines and array data change to reflect this. This doesn't seem overly difficult since I already have nearly the same thing happening with the blue ball however, my drag and drop code is based upon a single sprite not a dynamically named sprite. This is my blue ball movement code.
    //Drag and Drop built in Functionality
    blueBall.addEventListener(MouseEvent.MOUSE_DOWN, startMove);
    function startMove(evt:MouseEvent):void {
    stage.addEventListener(MouseEvent.MOUSE_MOVE, pointMove);
    }

    function pointMove(e:MouseEvent):void{
    blueBall.x = mouseX;
    blueBall.y = mouseY;
    e.updateAfterEvent();
    }

    //Drop
    blueBall.addEventListener(MouseEvent.MOUSE_UP, stopMove);
    function stopMove(evt:MouseEvent):void{
    stage.removeEventListener(MouseEvent.MOUSE_MOVE, pointMove);

    //Make new Dot
    newCoordDot();

    points.push(new Array(blueBall.x, blueBall.y));
    trace(points);

    graphics.lineTo(blueBall.x,blueBall.y);
    trace(points.length,' rows in Points array.');
    //set pointsCur Past Fut
    pointsCur = points.length - 1;

    forBut.gotoAndStop(3);
    backBut.gotoAndStop(1);
    trace('points: ', points.length);
    trace('pointsCur: ', pointsCur);

    }
    As you can see, I'm using event listeners for the blue ball being clicked and then listening for the movement of the ball on stage. If I was to use this method for each red ball I'd have to create a whole bunch of functions with event listeners for all red balls defeating the purpose of making them dynamically named and bogging down the program more than I dare. I've tried making some kind of for loop inside the point mouse function before realize that I've reached the limit of my knowledge.

    Once again, If any of you could give me a hint or advice to point me in the right direction I would be sincerely greatful. Thank you all for your time.

    -enxion
    Attached Images Attached Images
    Attached Files Attached Files

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