A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: Adding onLoad and onEnterFrame handlers to dynamic clips?

  1. #1
    Junior Member
    Join Date
    Dec 2002
    Location
    _root.home.room.chair
    Posts
    23

    Adding onLoad and onEnterFrame handlers to dynamic clips?

    Hi.

    I'm making a game where a laser is fired upon pressing the ctrl key. The laser is attached using attachMovie. I want to define a variable to the clip upon it loading and make it movie across the screen upon enterFrame. I know how to make it go across the screen and define the variable but i'm having trouble figuring out how to do this using attached movie clips. I'm using code like:

    PHP Code:
    _root["laser"+lasercounter].onEnterFrame 
    but that doesn't work.

    Any ideas?

    Thanks a lot


  2. #2
    PHP Code:
    _root["laser"+lasercounter].onEnterFrame=function(){
        
    //do whatever


  3. #3
    Senior Member mbenney's Avatar
    Join Date
    Mar 2001
    Posts
    2,744
    or

    PHP Code:
    clip.onEnterFrame something;
    function 
    something() {
    // do something!

    this way you can command different clips with the same function
    n.b dont put parentheses when you call the function in that way

  4. #4
    Junior Member
    Join Date
    Dec 2002
    Location
    _root.home.room.chair
    Posts
    23
    Thanks for replying

    Does that mean that if i want to use attachMovie i can only use functions to do the various actions or is there another way?

    Thanks


  5. #5
    Senior Member mbenney's Avatar
    Join Date
    Mar 2001
    Posts
    2,744
    you can put a

    this.onEnterFrame=function() {
    //stuff
    }

    inside the clip, this will fire when it comes on stage

    or put a containter clip with actions referring to the _parent

  6. #6
    Junior Member
    Join Date
    Dec 2002
    Location
    _root.home.room.chair
    Posts
    23
    Thanks



    I've tried using a function for the laser but it still won't work and i can't figure out why

    Can you please have a look at it and see if you can spot the problem. It's probably simple but i can't see it.

    PHP Code:
    onClipEvent(load) {
        
    speed 10;
        
    lc 1;
        
    laserspeed 20;
        function 
    laserfunction() {
            
    _root["laser"+lc]._x += laserspeed;
            if(
    _root["laser"+lc] >= 600) {
                
    _root["laser"+lc].removeMovieClip();
            }
        }
    }

    onClipEvent(enterFrame) {
        if(
    Key.isDown(Key.LEFT)) {
            
    this._x -= speed;
        }
        if(
    Key.isDown(Key.RIGHT)) {
            
    this._x += speed;
        }
        if(
    Key.isDown(Key.UP)) {
            
    this._y -= speed;
        }
        if(
    Key.isDown(Key.DOWN)) {
            
    this._y += speed;
        }
        if(
    Key.isDown(Key.CONTROL)) {
            
    attachMovie("laser","Laser"+lc,lc)
            
    _root["laser"+lc]._x spaceShip._x;
            
    _root["laser"+lc]._y spaceShip._y;
            
    _root["laser"+lc].onEnterFrame laserfunction();
            
    lc++;
        }

    Thanks a lot


  7. #7
    avatar free
    Join Date
    Jul 2002
    Location
    UK
    Posts
    835
    mbenney
    n.b dont put parentheses when you call the function in that way
    CTT_Mustapha
    _root["laser"+lc].onEnterFrame = laserfunction();
    also, the laserfunction function won't know what "lc" is, i think (but not 100% sure) you should just use

    this._x += laserspeed; etc

    also, there should be an "_x" >= 600 in the if statement in the laserfunction function methinks???
    jonmack
    flash racer blog - advanced arcade racer development blog

  8. #8
    avatar free
    Join Date
    Jul 2002
    Location
    UK
    Posts
    835
    also...

    "Laser"+lc and "laser"+lc will be different things i think (can't remember if instance names are case sensitive (not done much flash lately and i'm too lazy to check for you... sorry! )
    jonmack
    flash racer blog - advanced arcade racer development blog

  9. #9
    Junior Member
    Join Date
    Dec 2002
    Location
    _root.home.room.chair
    Posts
    23
    Thanks everybody

    I still can't get it to work but i'll keep trying.

    Your help is much appreciated


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