A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: [CS3] AS2 - Control Hero Movieclip with Movieclip Buttons

  1. #1
    Rookie
    Join Date
    Nov 2008
    Location
    Southern Ca
    Posts
    17

    [CS3] AS2 - Control Hero Movieclip with Movieclip Buttons

    Hello,

    Originally, the client wanted to control "hero_mc" using the keyboard but now they would like to control him with buttons only. Here is the original code on the "hero_mc" which works as expected:

    onClipEvent (load) {
    //-----
    speed = 10;
    scrolldistance = 3;
    //-----
    facing = 0;
    moviewidth = Stage.width;
    movieheight = Stage.height;
    rightsensor = (moviewidth - ((moviewidth / 10) * scrolldistance));
    leftsensor = ((moviewidth / 10) * scrolldistance);
    fg5 = speed * 1.5;
    fg4 = speed * 1.4;
    fg3 = speed * 1.3;
    fg2 = speed * 1.2;
    fg1 = speed * 1.1;
    playerspd = speed;
    bg1 = speed * 0.9;
    bg2 = speed * 0.8;
    bg3 = speed * 0.7;
    bg4 = speed * 0.6;
    bg5 = speed * 0.5;
    dbg = speed * 0.1;
    }
    onClipEvent (enterFrame) {
    if (_root.background_mc._x<=-3620) {
    _root.background_mc._x = -3620;
    } else if (_root.background_mc._x>=22) {
    _root.background_mc._x = 22;
    }
    if (Key.isDown(Key.LEFT)) {
    this.gotoAndStop("walk_left");
    if (_x <= leftsensor) {
    _root.background_mc._x += bg3;
    } else {
    _x -= playerspd;
    }
    }
    if (Key.isDown(Key.RIGHT)) {
    this.gotoAndStop("walk_right");
    if (_x >= rightsensor) {
    _root.background_mc._x -= bg3;
    } else {
    _x += playerspd;
    }
    }
    if (!Key.isDown(Key.LEFT) && !Key.isDown(Key.RIGHT)){
    this.gotoAndStop("rest");
    }

    }

    I have created 2 movieclip buttons on the stage called "left_nav_button_mc" and rt_nav_button_mc". Unfortunately, I have been unsuccessful trying to add a "left_nav_button_mc.onPress" function and a "rt_nav_button_mc.onpress" function to the above code. Should I declare a variable for each onPress and then call the variable in the "if" statements above? Any help would be appreciated.

    Thanks

  2. #2
    :
    Join Date
    Dec 2002
    Posts
    3,518

    Maybe you can adapt this to what you are trying to do...

    Code:
    onClipEvent (enterFrame) {
    	if (_root.background_mc._x <= -3620) {
    		_root.background_mc._x = -3620;
    	} else if (_root.background_mc._x >= 22) {
    		_root.background_mc._x = 22;
    	}
    	if (Key.isDown(Key.LEFT) || _root.toLeft) {
    		this.gotoAndStop("walk_left");
    		if (_x <= leftsensor) {
    			_root.background_mc._x += bg3;
    		} else {
    			_x -= playerspd;
    		}
    	}
    	if (Key.isDown(Key.RIGHT) || _root.toRight) {
    		this.gotoAndStop("walk_right");
    		if (_x >= rightsensor) {
    			_root.background_mc._x -= bg3;
    		} else {
    			_x += playerspd;
    		}
    	}
    	if (!Key.isDown(Key.LEFT) && !Key.isDown(Key.RIGHT) || (!_root.toLeft && !_root.toRight)) {
    		this.gotoAndStop("rest");
    	}
    }
    Left button
    Code:
    on (press) {
    	_root.toLeft = true;
    	trace(_root.toLeft)
    }
    on (release, releaseOutside) {
    	_root.toLeft = false;
    }
    Right button
    Code:
    on (press) {
    	_root.toRight = true;
    }
    on (release, releaseOutside) {
    	_root.toRight = false;
    }

  3. #3
    Rookie
    Join Date
    Nov 2008
    Location
    Southern Ca
    Posts
    17
    Awesome! This works great! Thank you very much for your time.

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