Go Back   CodingForums.com > :: Client side development > Flash & ActionScript

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-30-2012, 08:31 AM   PM User | #1
Orbytel
New to the CF scene

 
Join Date: May 2012
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Orbytel is an unknown quantity at this point
Issue with increasing speed of MC

Hi all,

I was wondering if someone could assist with an issue I'm having. I'm creating an interactive menu planner, which has a moving list of foods that move along a conveyer belt.

The user can hit a 'stop' button, that will stop the foods on the belt, which makes the food items drag-and-dropable.

Once they have selected the foods they want, they hit the 'go' button, and the foods begin moving along the belt again.

I've implemented it, and having running, but the issue I've run into is that every time I hit the 'go' button the speed of all the items increases ten fold. I've tried using timers, enter frame etc and all result in the same issue.

If someone could let me know where I'm going wrong, or a potential solution, it would be greatly appreciated.

Thank you in advance- and I apologise for the lack of commenting.

Code:
import flash.events.MouseEvent;
import flash.events.Event;
import flash.display.MovieClip;
 
back_btn.addEventListener(MouseEvent.CLICK, back);
colorBox.alpha = 1;
eyeColour.alpha = 1;
week_txt.text = "1";
score_txt.text = "0";
dropbox.alpha = 0;
var currentScore:int=0;
var stopFlag:Boolean = false;
go_btn.alpha = 0;
go_btn.enabled = false;
beltRun();
 
var foodArray:Array = new Array();
var currentArray:int = 0;
var foodProps:Dictionary = new Dictionary(true);
var attributes:Object = new Object(); 
 
 
/* Issue occurs in this function */
 
function beltRun()
{
    addEventListener(Event.ENTER_FRAME, beltTimer);
 
    function beltTimer(event:Event):void
    {
        if (stopFlag == false)
        {
            for (var j:int = 0; j < foodArray.length; j++)
        {    
            var logoManip:MovieClip;
            logoManip = foodArray[j] as MovieClip;
                    
            logoManip.x = (logoManip.x + 3);//this is what is used to move the mc. 
                    
            trace("Go!");
            if (logoManip.x > stage.stageWidth)
            {
                logoManip.x = 0;
            }
        }
        }
        if (stopFlag == true)
        {
            for (var i:int = 0; i < foodArray.length; i++)
            {
                trace("Stop!");
                foodTrain();
            }
        }
    }
}

 
stop_btn.addEventListener(MouseEvent.CLICK, stopBelt);
go_btn.addEventListener(MouseEvent.CLICK, stopBelt);
 
function stopBelt(event:MouseEvent):void
{
    if(stopFlag == true)
    {
        stopFlag = false;
        go_btn.alpha = 0;
        go_btn.enabled = false;
        stop_btn.alpha = 1;
        stop_btn.enabled = true;
    }
    else
    {
        stopFlag = true;
        stop_btn.alpha = 0;
        stop_btn.enabled = false;
        go_btn.alpha = 1;
        go_btn.enabled = true;
    }
    beltRun();
}
 
var fastForwardTimer:Timer = new Timer(1100,4);
 
fastForwardTimer.addEventListener(TimerEvent.TIMER, timer);
 
fastForwardTimer.start();
 
function timer(event:TimerEvent):void
{
    addFood();
}
 
function addFood():void
{
    var currentFood;
    switch(currentArray)
    {
        case 0:
        var appleFood:apple = new apple();
        currentFood = appleFood;
        attributes.sugar = 5;
        attributes.score = 2;
        trace("test apple");    
        break;
            
        case 1:
        var juiceFood:juice = new juice();
        currentFood = juiceFood;
        trace("test juice");    
        attributes.sugar = 3;
        attributes.score = 5;
        trace("Sugar level " + attributes.sugar + " Score " + attributes.score)
        break;
        
        case 2:
        var bagelFood:bagel = new bagel();
		currentFood = bagelFood;
        attributes.sugar = 5;
        attributes.score = 2;
        trace("test bagel");    
        break;
            
        case 3:
        var cakeFood:cake = new cake();
        currentFood = cakeFood;
        attributes.sugar = 5;
        attributes.score = 2;
        trace("test cake");
        
        default:
        trace("None of the above were met");
    }
    addChild(currentFood);
 
    //Once the child has been added, change the following attributes
    currentFood.x += 58;
    currentFood.y += 347;
    setChildIndex(currentFood, numChildren-1);
    foodArray.push(currentFood);
 
    foodProps[currentFood] = attributes;
    currentArray++;
}
 
function foodTrain()
{
    for (var i:int = 0; i < foodArray.length; i++) 
    {    
        var logoManip:MovieClip;
        var startX:Number;
        var startY:Number;
        logoManip = foodArray[i] as MovieClip;
        logoManip.buttonMode = true;
        var currentClip:MovieClip;
        
        if (stopFlag == true)
        {
            logoManip.addEventListener(MouseEvent.MOUSE_DOWN, item_onMouseDown);
            
            function item_onMouseDown(event:MouseEvent):void 
            {
                currentClip = MovieClip(event.currentTarget);
                startX = currentClip.x;
                startY = currentClip.y;
                addChild(currentClip); 
                currentClip.startDrag();
                stage.addEventListener(MouseEvent.MOUSE_UP, stage_onMouseUp);
            }
            
            function stage_onMouseUp(event:MouseEvent):void 
            {
                if(currentClip.hitTestObject(dropbox)) 
                {
                    attributes = foodProps[logoManip];
                    trace(attributes.score);
                    currentScore = (currentScore + attributes.score);
                    trace(currentScore);
                    //a match was made! position the clip on the matching clip:
                    currentClip.x = dropbox.x;
                    currentClip.y = dropbox.y;
                    currentClip.alpha = 0;
                    //make it not draggable anymore:
                    currentClip.removeEventListener(MouseEvent.MOUSE_DOWN, item_onMouseDown);
                    currentClip.buttonMode = false;
    
                } 
                else 
                {
                    currentClip.x = startX;
                    currentClip.y = startY;
                }
                currentClip.stopDrag();
            }
        }
    }//end for
    score_txt.text = String(currentScore);
}//end function

Last edited by Orbytel; 05-30-2012 at 08:33 AM..
Orbytel is offline   Reply With Quote
Old 05-30-2012, 02:34 PM   PM User | #2
djpaul1963
New Coder

 
djpaul1963's Avatar
 
Join Date: Apr 2012
Location: Athens GR
Posts: 65
Thanks: 0
Thanked 8 Times in 8 Posts
djpaul1963 is an unknown quantity at this point
function beltRun()
{
addEventListener(Event.ENTER_FRAME, beltTimer);


Because beltRun is called on every button click and because you have the same listener on stop and go clicks (you shouldn't btw), each time you click a button, a new ENTER_FRAME is created with the same action, so actually, on every frame you have the movement multiplied by the times you clicked the button so far.
Remove the listener from there and leave only the stop flag comparison for the rest of the function.

Last edited by djpaul1963; 05-30-2012 at 02:39 PM..
djpaul1963 is offline   Reply With Quote
Users who have thanked djpaul1963 for this post:
Orbytel (05-31-2012)
Old 05-31-2012, 03:16 AM   PM User | #3
Orbytel
New to the CF scene

 
Join Date: May 2012
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Orbytel is an unknown quantity at this point
Cheers,

Fixed the issue- can't believe I missed that!
Orbytel is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 +1. The time now is 01:13 PM.


Advertisement
Log in to turn off these ads.