A Flash Developer Resource Site

Page 1 of 2 12 LastLast
Results 1 to 20 of 24

Thread: Check if a guy is to the left or right of you....??

  1. #1
    Amiga freak Ironclaw's Avatar
    Join Date
    Jan 2001
    Location
    Sweden
    Posts
    1,650

    Check if a guy is to the left or right of you....??

    I have a tiny little problem.....

    Code:
    onClipEvent (enterFrame) {
    	if (_root.guy.highkick.highkick.hitTest(_parent)) {
    		Hita = new Sound();
    		Hita.attachSound("Hita");
    		Hita.start();
    		_root.boss -= 1;
    		_parent._x -= 5;
    	}
    }
    The above code is located on a MC in my boss MC. "guy" is the player. So.... if guy hits the boss, a hit sound is played, 1 HP is drawn from the boss' health and the boss is moved 5 pixels back if boss is facing right which means that "guy" is to the right......

    My problem is, how can the boss tell if the "guy" is to the left or right of him?

    if "guy" is to the left, then "_parent._x += 5;", else if "guy" is to the right, then "_parent._x -= 5;"



    thanks.

  2. #2
    Who needs pants? hooligan2001's Avatar
    Join Date
    Apr 2001
    Location
    Somewhere
    Posts
    1,976
    Wouldnt you just compare the _x values if its greater its to the right if its less then its to the left.
    Its just a thought

  3. #3
    Amiga freak Ironclaw's Avatar
    Join Date
    Jan 2001
    Location
    Sweden
    Posts
    1,650
    Yepp, that's the way to do it.... I just couldn't get the correct coding.

  4. #4
    Who needs pants? hooligan2001's Avatar
    Join Date
    Apr 2001
    Location
    Somewhere
    Posts
    1,976
    Code:
    onClipEvent(enterFrame){
    if(_root.boss._x >= _x){
     //i am right
    }
    if(_root.boss._x <= _x){
     //i am left
    }
    }
    Thats about it

  5. #5
    Who needs pants? hooligan2001's Avatar
    Join Date
    Apr 2001
    Location
    Somewhere
    Posts
    1,976
    Just noticed something

    change
    Code:
    onClipEvent(enterFrame){
    if(_root.boss._x >= _x){
     //i am right
    }
    if(_root.boss._x <= _x){
     //i am left
    }
    }
    too

    Code:
    onClipEvent(enterFrame){
    if(_root.boss._x >= _x+1){
     //i am right
    }
    if(_root.boss._x <= _x-1){
     //i am left
    }
    }

  6. #6
    Yes we can tomsamson's Avatar
    Join Date
    Sep 2001
    Location
    Team Titan Secret Lair
    Posts
    4,666
    hey there ironclaw,check your game´s fla,i already added such a script into the movement code of the normal enemies.
    (if the player is right of enemies,they move right..)

  7. #7
    Who needs pants? hooligan2001's Avatar
    Join Date
    Apr 2001
    Location
    Somewhere
    Posts
    1,976
    Yeah just ignore everything i tell ya eh, listen to lord tomsamson

    It would probally be the smartest thing to do

  8. #8
    Amiga freak Ironclaw's Avatar
    Join Date
    Jan 2001
    Location
    Sweden
    Posts
    1,650
    Yepp, tomsamson, That was the first thing I looked at when I came up with the idea..... couldn't work it out though..


    In the code I got from you Hooligan, you had "root.boss._x", _root.boss is the boss' health .


    Tried something like this, but where ever I stand, left or right, the boss moves 5 pixels to the right..


    Code:
    onClipEvent (enterFrame) {
    	if (_root.guy.highkick.highkick.hitTest(_parent) and _root.guy._x>=_x+1) {
    		Hita = new Sound();
    		Hita.attachSound("Hita");
    		Hita.start();
    		_root.boss -= 1;
    		_parent._x += 5;
    	}
    	if (_root.guy.highkick.highkick.hitTest(_parent) and _root.guy._x<=_x-1) {
    		Hita = new Sound();
    		Hita.attachSound("Hita");
    		Hita.start();
    		_root.boss -= 1;
    		_parent._x -= 5;
    	}
    }
    Last edited by Ironclaw; 05-24-2003 at 10:46 AM.

  9. #9
    Amiga freak Ironclaw's Avatar
    Join Date
    Jan 2001
    Location
    Sweden
    Posts
    1,650
    Only need this.... with a left and right check.

    Code:
    onClipEvent (enterFrame) {
    	if (_root.guy.highkick.highkick.hitTest(_parent) and _root.guy._x>=_x) {
    		_parent._x += 5;
    	}
    	if (_root.guy.highkick.highkick.hitTest(_parent) and _root.guy._x<=_x) {
    		_parent._x -= 5;
    	}
    }
    Also tried "_root.guy._x<=_parent" as the code is on a MC.

    wont work.

  10. #10
    Amiga freak Ironclaw's Avatar
    Join Date
    Jan 2001
    Location
    Sweden
    Posts
    1,650
    Arghh!! I give up... tried:

    Code:
    _root.guy._x>=_x
    Code:
    _root.guy._x>_x
    Code:
    _root.guy._x>=_parent._x
    Code:
    _root.guy._x>_parent._x
    Code:
    _root.guy._x>=this._x
    Code:
    _root.guy._x>this._x
    Also tried all the above but with +1 and +5 after the _x.

    and "}else if" instead of "if"

    Code:
    onClipEvent (enterFrame) {
    	if (_root.guy.highkick.highkick.hitTest(_parent) and _root.guy._x>=_x) {
    		_parent._x += 5;
    	}
    	}else if (_root.guy.highkick.highkick.hitTest(_parent) and _root.guy._x<=_x) {
    		_parent._x -= 5;
    	}
    }

  11. #11
    Amiga freak Ironclaw's Avatar
    Join Date
    Jan 2001
    Location
    Sweden
    Posts
    1,650
    Also tried all combinations with this:

    Code:
    onClipEvent (enterFrame) {
    	if (_root.guy.highkick.highkick.hitTest(_parent) and _parent._x>=_root.guy._x) {
    		_parent._x += 5;
    	}
    	if (_root.guy.highkick.highkick.hitTest(_parent) and _parent._x<=_root.guy._x) {
    		_parent._x -= 5;
    	}
    }
    using tomsamson's

    walking script:


    Code:
    function move(){
    	walk1=1.5;
    	walk2=1.5;
    point = new object();
    point.x = _root.Guy._x
    point.y = _root.Guy._y;
    _root.bg.globalToLocal(point);
    	if(_x<point.x){
    		_xscale=100;
    	this._x += walk1;
    }
    	if(_x>point.x){
    		_xscale=-100;
    	this._x -= walk2;
    }
    
    
    }
    move();
    This script uses "if enemy is < of guy"


    The others in this post uses "if guy is < of enemy"

    So no wonder it doesn't work, it's just reversed.... anyway..... all of the scripts I've posted should work...except the first one.... can't understand why they don't

  12. #12
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    Originally posted by Ironclaw
    Also tried all combinations with this:

    Code:
    onClipEvent (enterFrame) {
    	if (_root.guy.highkick.highkick.hitTest(_parent) and _parent._x>=_root.guy._x) {
    		_parent._x += 5;
    	}
    	if (_root.guy.highkick.highkick.hitTest(_parent) and _parent._x<=_root.guy._x) {
    		_parent._x -= 5;
    	}
    }
    It has to work as long you actually move _root.guy so its _x changes.
    If you cant get it work, use trace
    Code:
    onClipEvent (enterFrame) {
    	if (_root.guy.highkick.highkick.hitTest(_parent) and _parent._x>=_root.guy._x) {
                    trace(_parent._x);
                    trace(_root.guy._x);
    		_parent._x += 5;
    	}
    	if (_root.guy.highkick.highkick.hitTest(_parent) and _parent._x<=_root.guy._x) {
                    trace(_parent._x);
                    trace(_root.guy._x);
    		_parent._x -= 5;
    	}
    }

  13. #13
    Amiga freak Ironclaw's Avatar
    Join Date
    Jan 2001
    Location
    Sweden
    Posts
    1,650
    Tried trace before.... Only the first "if" is executed... so the enemy is moved +5 pixels... not the other one...


    I will make a example fla for you all to look at and maybe try to fix.

    Later.... going out now.

  14. #14
    ism BlinkOk's Avatar
    Join Date
    Aug 2001
    Location
    , location, location
    Posts
    5,002
    if the boss mc is facing upwards or downwards within it's mc then try this;
    Code:
    onClipEvent (enterFrame) {
     var p = { x: 0, y: 0 };
     _root.guy.localToGlobal(p);
     globalToLocal(p);
     if (p.x > 0)
       // Guy is on the right
     else
      // Guy is on left or directly in front
    }
    Graphics Attract, Motion Engages, Gameplay Addicts
    XP Pro | P4 2.8Ghz | 2Gb | 80Gb,40Gb | 128Mb DDR ATI Radeon 9800 Pro

  15. #15
    Amiga freak Ironclaw's Avatar
    Join Date
    Jan 2001
    Location
    Sweden
    Posts
    1,650
    Localtoglobal should not be neccesary, I think...... all the enemy needs to do is to see if the guy is to the left or right.... I tried your code but there's no change....

    Brobably used it wrong...


    Code:
    onClipEvent (enterFrame) {
    	var p = {x:0, y:0};
    	_root.guy.localToGlobal(p);
    	globalToLocal(p);
    	if (p.x>0) {
    		if (_root.guy.highkick.highkick.hitTest(_parent)) {
    			_parent._x -= 5;
    		} else if (_root.guy.highkick.highkick.hitTest(_parent)) {
    			_parent._x += 5;
    		}
    	}
    }

    Anyway. I have made a little example fla. The boss' code is located inside MC on the left side of the stage and on keyframe 4 above his head.

  16. #16
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    Works for me
    Only made it for highkick, but boss DOES move back.

  17. #17
    Amiga freak Ironclaw's Avatar
    Join Date
    Jan 2001
    Location
    Sweden
    Posts
    1,650
    Damn.. it work in the example fla but not when applying it to the game... Maybe I DO need to use the localtoglobal thing...


    here is a new fla using the attachment... (ehh, the attach method in Flash that is)


    press the green button to attach the boss.
    Last edited by Ironclaw; 05-25-2003 at 05:58 AM.

  18. #18
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    Yeah, you have boss inside bg mc and bc is moved, so you could use local to global.

    Or you could use simple way for people who dont use localtoglobal magic (like me) and add bg mc x coordinate to if statement.
    Code:
            if (_parent._x+_parent._parent._x<_root.guy._x) {
                _parent._x -= 5;
            } else {
                _parent._x += 5;
            }
    Hope this helped

  19. #19
    Amiga freak Ironclaw's Avatar
    Join Date
    Jan 2001
    Location
    Sweden
    Posts
    1,650
    Thanks, tonypa... .

    Could you please try to solve another little problem? hehe


    It's just that when I kick the enemy, I want to take away 1 hp of the enemy's hp. The problem is.... when kicking him 1 hp is removed but while the leg is still touching him before it is pulled back, it removes anything from 1 to 3 hp.

    How can I make it remove only 1 hp?

    Code:
    onClipEvent (enterFrame) {
    	if (_root.guy.highkick.highkick.hitTest(_parent)) {
    		if (_parent._x+_parent._parent._x<_root.guy._x) {
    			Hita = new Sound();
    			Hita.attachSound("Hita");
    			Hita.start();
    			_root.boss -= 1;
    			_parent._x -= 5;
    		} else {
    			Hita = new Sound();
    			Hita.attachSound("Hita");
    			Hita.start();
    			_root.boss -= 1;
    			_parent._x += 5;
    		}
    	}
    }
    Another fla has been attached, with the health of the boss.



  20. #20
    Amiga freak Ironclaw's Avatar
    Join Date
    Jan 2001
    Location
    Sweden
    Posts
    1,650
    I could solve it in a uneccesary complicated way involving two new MCs and a few variables..


    Maybe someone know of a... more cleverly way?

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