A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: Odd Function Problem, HELP!

  1. #1
    Senior Member
    Join Date
    Apr 2007
    Posts
    269

    Unhappy Odd Function Problem, HELP!

    Hi I have set up a system that spawns barriers for the player to dodge but it appears the functions won't trigger properly from within "if" statements. I've put nots on my code to guide you through what is going on. PLEASE HELP ME!!!
    PHP Code:
    var speed:Number=2;
    var 
    barNumber:int=6;
    var 
    barLoop:int=0;
    var 
    timer:Number=0;
    var 
    limit:Number=100;

    //Works (Spawns Barrier and moves)
    spawn();

    //Doesn't work (Spawns barriers but doesn't move)
    //Runs every frame
    function timedSpawn():void{
        if(
    timer >= limit){
            
    spawn();
            
    //Resets timer
            
    timer 0;
        }else{
            
    timer += 1;
        }
    }

    //Run once when barrier needs to be created
    function spawn(){
        
    //Random side of the screen
        
    var screen_side:Number=Math.round(Math.random()*3);
        
    //Library reference
        
    var barTemp:MovieClip=new bar;
        
    //Give MovieClip a number
        
    this["bar"+barLoop]=barTemp;
        
    //Add new movieclip to stage
        
    addChild(this["bar" barLoop]);
        
    //Individual sides saved for movement later
        
    this["side"+barLoop]=screen_side;
        
    //X/Y and Rotation Depending on the Randomly Generated Screen Side
        //Right
        
    if (screen_side==0) {
            
    this["bar"+barLoop].x=820;
            
    this["bar"+barLoop].y=Math.round(Math.random()*(500-100))+100;
            
    this["bar"+barLoop].rotation 90;
        }
        
    //Left
        
    if (screen_side==1) {
            
    this["bar"+barLoop].x=-20;
            
    this["bar"+barLoop].y=Math.round(Math.random()*(500-100))+100;
            
    this["bar"+barLoop].rotation 90;
        }
        
    //Top
        
    if (screen_side==2) {
            
    this["bar"+barLoop].x=Math.round(Math.random()*(700-100))+100;
            
    this["bar"+barLoop].y=-20;
        }
        
    //Bottom
        
    if (screen_side==3) {
            
    this["bar"+barLoop].x=Math.round(Math.random()*(700-100))+100;;
            
    this["bar"+barLoop].y=620;
        }
        
    //Change number for next barrier
        
    barLoop += 1;
    }
    //Runs every frame
    function moveBarriers():void {
        
    //Resets the loop to run through all the barriers on stage again
        
    barLoop=0;
        
    //Moves each individual barrier on stage in a direction dependant on the side they spawned on
        
    while (barLoop barNumber) {
            if(
    this["side"+barLoop]==0){
                
    this["bar" barLoop].-= speed;
            }
            if(
    this["side"+barLoop]==1){
                
    this["bar" barLoop].+= speed;
            }
            if(
    this["side"+barLoop]==2){
                
    this["bar" barLoop].+= speed;
            }
            if(
    this["side"+barLoop]==3){
                
    this["bar" barLoop].-= speed;
            }
            
    barLoop=barLoop+1;
        }

    Please ask me any questions if you still can't understand my script. I really need help with this.

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Your code is very oddly organized. Can you rephrase, in plain english, what you're trying to do?

    It looks like you're trying to spawn a barrier every frame, and move existing barriers around somehow. But using the this["bar"+index] method of addressing properties is pretty awkward and as2-ish.

  3. #3
    Senior Member
    Join Date
    Apr 2007
    Posts
    269
    I'm trying to run the spawn function every time the timer reaches the limit.

    if the function is run from the begin it works but if it is triggered during later at run time it will spawn but won't move.

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Well, your timer isn't a Timer, it's just a number. And I don't see you calling timedSpawn anywhere.

    This is definitely frame script, so I'm assuming this frame is being looped a lot. I wouldn't do it that way, but it's not wrong per se.

    What I'd do is set up an actual Timer, and use a variation of timedSpawn as a listener on TimerEvent.TIMER. Moving the barriers could be done on an enterframe listener. Definitely change the this["bar"+i] stuff to use a proper array.

  5. #5
    Senior Member
    Join Date
    Apr 2007
    Posts
    269
    "//Runs every frame " the add eventlisteners in in the other doc
    PHP Code:
    var t5:MovieClip = new trail5;
    addChild(t5);
    var 
    t4:MovieClip = new trail4;
    addChild(t4);
    var 
    t3:MovieClip = new trail3;
    addChild(t3);
    var 
    t2:MovieClip = new trail2;
    addChild(t2);
    var 
    t1:MovieClip = new trail1;
    addChild(t1);
    var 
    player:MovieClip = new player_art;
    addChild(player);
    var 
    cursor:MovieClip = new cursor_art;
    addChild(cursor);
    //Including the AS files
    include "AS/rockets_imp.as"

    var XS:Number 0;
    var 
    YS:Number 0;
    var 
    ACC:Number 4;
    var 
    DEC:Number .8;
    var 
    vBox:Number 8;
    var 
    trail_offset:Number 2;
    var 
    key:KeyObject = new KeyObject(stage);

    stage.addEventListener(Event.ENTER_FRAMEeachFrame);
    Mouse.hide();

    function 
    eachFrame(e:Event):void{
        
    //HERE!!!
        
    timedSpawn();
        
    moveBarriers();
        
    cursor.mouseX;
        
    cursor.mouseY;
        
    player.+= XS;
        
    player.+= YS;
        if(
    mouseX player.-vBox){
            
    XS += ACC;
        }
        if(
    mouseX player.+vBox){
            
    XS -= ACC;
        }
        if(
    mouseY player.-vBox){
            
    YS += ACC;
        }
        if(
    mouseY player.+vBox){
            
    YS -= ACC;
        }
        
    t1.-= (t1.x-player.x) / trail_offset;
        
    t1.-= (t1.y-player.y) / trail_offset;
        
    t2.-= (t2.x-t1.x) / trail_offset;
        
    t2.-= (t2.y-t1.y) / trail_offset;
        
    t3.-= (t3.x-t2.x) / trail_offset;
        
    t3.-= (t3.y-t2.y) / trail_offset;
        
    t4.-= (t4.x-t3.x) / trail_offset;
        
    t4.-= (t4.y-t3.y) / trail_offset;
        
    t5.-= (t5.x-t4.x) / trail_offset;
        
    t5.-= (t5.y-t4.y) / trail_offset;
        
    XS *= DEC;
        
    YS *= DEC;

    Could you explain how I can do the array instead?
    Last edited by unrealhacker12; 02-03-2009 at 06:17 PM.

  6. #6
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I'll just link neznein9's blog post about names and arrays, since that's what he wrote it for:
    http://www.calypso88.com/?p=302

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