A Flash Developer Resource Site

Results 1 to 12 of 12

Thread: Code translation please

  1. #1
    Senior Member
    Join Date
    May 2006
    Posts
    119

    Code translation please

    I was reading through a tute on collission detection and I found what I think Im looking for. ( detecting collission when an object is in a "drag' status)

    but I dont understand it....its slash syntax...I'm a newbie to dot sytax!! I'm not certain what all these become in MX 2004. could anyone translate for me?

    this is a quote from the tute:
    Code:
    7. Now in the first frame of your ENEMY movie, enter the following code:
    
    flag = this.hitTest("/ship"); if (flag == true) { tellTarget ("/") { gotoAndStop
     ("hit"); } } You do this by selecting setVariable from the Actions menu. Then in the Variable section, enter: flag 
    
    and in the in the Value section, enter: this.hitTest("/ship")
    
    8. Then after that line of code, enter the following:
    
    if (flag == true) { tellTarget ("/ship") { gotoAndStop (2); } } This is the code that will tell your ship movie to explode upon collision.
    
    9. That's really all there is to it! Have fun!
    This is what I was working with before I went searching. I have this on my drag object

    Code:
    on(press){
    	startDrag(this);
    	if(_root.pin.hitTest(_root.balloon)){
    		_root.balloon.balloon2.gotoAndPlay("popped");
    	}
    }
    
    on(release){
    	stopDrag();
    	}
    this code works if I have the if statement on the RELEASE event, but not on the drag event.......

    I hope thats enough explanation.

    Thank you in advance for any help

    Mark

  2. #2
    Patron Saint of Beatings WilloughbyJackson's Avatar
    Join Date
    Nov 2000
    Location
    Ro-cha-cha-cha, New York
    Posts
    1,988
    Well, basically it's because the onPress/onRelease event only happens once.

    I recommend doing something like this (NOTE: I'm assuming this code is from a button inside of a movie clip):

    code:

    on(press){
    startDrag(this);
    this.onMouseMove = function(){
    if(_root.pin.hitTest(_root.balloon)){
    _root.balloon.balloon2.gotoAndPlay("popped");
    }
    }
    }

    on(release){
    stopDrag();
    this.onMouseMove = undefined
    }




    EDIT: Oh yeah. / = _root
    Last edited by WilloughbyJackson; 06-22-2006 at 12:30 PM.

  3. #3
    Senior Member
    Join Date
    May 2006
    Posts
    119
    Cool! it worked! now I want to ask, what is it? could you break it down for me? And maybe what some common application of this are? I feel as if this will be an important function.

    Thank you very much

    Mark

  4. #4
    Patron Saint of Beatings WilloughbyJackson's Avatar
    Join Date
    Nov 2000
    Location
    Ro-cha-cha-cha, New York
    Posts
    1,988
    Well, this is basically a drag and drop routine if you need to check something while dragging.

    To break it down:
    code:

    on(press){
    startDrag(this);
    //This creates a function watch for onMouseMove events. That means whenever the
    //mouse is moved, whatever is inside of this function will occur.
    this.onMouseMove = function(){
    //For now, it is your hittest, however, you add as much code as you like in this statement.
    if(_root.pin.hitTest(_root.balloon)){

    _root.balloon.balloon2.gotoAndPlay("popped");

    }

    }

    }



    on(release){

    stopDrag();
    //Setting this.onMouseMove to undefined is erasing whatever checks were set up in
    //onPress statement.

    this.onMouseMove = undefined

    }




    I've used something like this for mouse based games before.

  5. #5
    Senior Member
    Join Date
    May 2006
    Posts
    119
    Ok, I think I understand that now. But I have one more question. Now I am trying to make a pin pop many balloons. my popped balloons have an animation for when they are popped. I found with this script I could pop a balloon, but if i kept moving the mouse, it would restart the animation. I corrected that by adding a

    delete this.onMouseMove;

    it worked for one MC.
    now I have 4 balloons, when I pooped one, it would delete the onMouseMove so I could not pop any more.
    I figured I would add the this.onMouseMove = function(); to the top of each if statement to see if it would work. now it will only pop the last balloon in my if statement.
    I'm out of ideas......
    here is what I have. (probably no very good)

    Code:
    on(press){
    	Mouse.hide();
    	startDrag(this, true);
    this.onMouseMove = function(){
    	if(_root.pin.hitTest(_root.r_balloon)){
    		_root.r_balloon.gotoAndPlay("popped");
    		delete this.onMouseMove
    	}
    	}
    	this.onMouseMove = function(){
    	if(_root.pin.hitTest(_root.b_balloon)){
    		_root.b_balloon.gotoAndPlay("popped");
    		_root.b_balloon._visible = false;
    		delete this.onMouseMove
    	}
    	}
    	this.onMouseMove = function(){
    	if(_root.pin.hitTest(_root.g_balloon)){
    		_root.g_balloon.gotoAndPlay("popped");
    		delete this.onMouseMove
    	}
    	}
    	this.onMouseMove = function(){
    	if(_root.pin.hitTest(_root.p_balloon)){
    		_root.p_balloon.gotoAndPlay("popped");
    		delete this.onMouseMove
    		}
                }
    }
    on(release){
    	stopDrag();
    this.onMouseMove = undefined
    	}

  6. #6
    Patron Saint of Beatings WilloughbyJackson's Avatar
    Join Date
    Nov 2000
    Location
    Ro-cha-cha-cha, New York
    Posts
    1,988
    First comment: You do not have to delete the onMouseMove after each hit test, you can just have the hit tests listed in sequence.

    I.E.
    code:

    //...SNIP...
    this.onMouseMove = function(){
    if(_root.pin.hitTest(_root.r_balloon)){
    _root.r_balloon.gotoAndPlay("popped");
    }
    if(_root.pin.hitTest(_root.b_balloon)){
    _root.b_balloon.gotoAndPlay("popped");
    _root.b_balloon._visible = false;
    }
    if(_root.pin.hitTest(_root.g_balloon)){
    _root.g_balloon.gotoAndPlay("popped");
    }
    if(_root.pin.hitTest(_root.p_balloon)){
    _root.p_balloon.gotoAndPlay("popped");
    }
    }
    }
    //...SNIP...



    Or you could use an if else chain here.

    However, to make things easier, this is where you would use For loops and concatenation.

    First, rename your balloon movie clips to:
    balloon_0
    balloon_1
    balloon_2
    balloon_3

    Next you would set up a for loop and use concatenation to check each
    of the balloons.

    Concatenation is the operation of joining two character strings end to end or a string and variable.

    In otherwords if you have this:
    code:

    var i = 0

    _root["balloon_"+i]



    is exactly like saying this:
    code:

    _root.balloon_0



    You follow me so far?

    Okay..here's the example:
    code:

    //...SNIP...
    this.onMouseMove = function(){
    //FOR LOOP CODE IS LIKE THIS
    //for (starting number;repeat this until this condition is no longer true;how
    //the number will increase. ++ in Flash means it will increase by 1 for //each time through the loop.
    for (var i = 0;i <= 3;i++){
    if(_root.pin.hitTest(_root["balloon_"+i])){
    _root["balloon_"+i].gotoAndPlay("popped");
    }

    }
    }
    //...SNIP...


  7. #7
    Senior Member
    Join Date
    May 2006
    Posts
    119
    sorry to take so long to get back to this thread. Thank you for the mini tute on for loops, I have read up on them before, but this has been the easiest to understand. I will definately make a n effort to get to know it better.


    Thanks again

    Mark

  8. #8
    Patron Saint of Beatings WilloughbyJackson's Avatar
    Join Date
    Nov 2000
    Location
    Ro-cha-cha-cha, New York
    Posts
    1,988
    Not a problem.

  9. #9
    Senior Member
    Join Date
    May 2006
    Posts
    119
    i have another question about the onMouseMove function. I did what you said and put in a For loop with the If statement. But the problem stil remains where due to the onMouseMove, it triggers my drag objects animation every time the mouse is moved on the hitTest target making a very un-clean animation. Im trying to have the animation play only once per object... I have the fla. attached, the code is all over the place (I'm a newbie so I do what I can, so if anyone has some code clean up suggestions, feel free.) there is some code on the frame labeled "splat" that I tried to write on the first frame with an onEnterFrame event handler, but it didn't work. so I put the code on the objects (the frame is about 3 levels deep if you go looking for it) and the little red box is just a timer to end the sequence (its part of a bigger quiz game) the box is usually not visible, but I did this so it could be found.
    Anyways, i know its a total mess, but its really the best i can do....
    Thanks for all the help
    ....wow, just uploaded the Fla, dont know why its so big. it was only 16k when it was a frame of my quiz game..strange
    Attached Files Attached Files

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

    Maybe try something like this...

    Code:
    onClipEvent (load) {
    	for (var i = 0; i<=5; i++) {
    		_root["fly_"+i].splat = false;
    	}
    }
    on (press) {
    	Mouse.hide();
    	startDrag(this, true);
    	this.onMouseMove = function() {
    		for (var i = 0; i<=5; i++) {
    			if (_root.fly_swatter.mesh.hitTest(_root["fly_"+i]) && _root["fly_"+i].splat == false) {
    				_root.fly_swatter.gotoAndPlay("swat");
    				_root["fly_"+i].splat = true;
    				_root["fly_"+i].fly2.gotoAndStop("splat");
    			}
    		}
    	};
    }
    on (release) {
    	stopDrag();
    	this.onMouseMove = undefined;
    }

  11. #11
    Senior Member
    Join Date
    May 2006
    Posts
    119
    OK, that worked great! Thank you
    I'm wondering if I could run by what I understand this script to be saying and could you tell me if im right or not. I'm still new to this so Im interested to know.

    onClipEvent (load) {
    for (var i = 0; i<=5; i++) {
    _root["fly_"+i].splat = false;
    }
    setting a all the flys booleen to false.

    for (var i = 0; i<=5; i++) {
    if (_root.fly_swatter.mesh.hitTest(_root["fly_"+i]) && _root["fly_"+i].splat == false) {
    _root.fly_swatter.gotoAndPlay("swat");
    _root["fly_"+i].splat = true;
    _root["fly_"+i].fly2.gotoAndStop("splat");
    we are now watching the flys and waiting to the IF condition to be met. The condition is IF the swatter is on a fly AND if the fly's booleen is false (meaning it hasn't been squashed) then swat the fly, change its booleen to true so that it can no longer trigger the animation and stop the playhead on the sqwashed fly anim.

    Thanks so much. I think I learnt something new here.

  12. #12
    :
    Join Date
    Dec 2002
    Posts
    3,518
    Yes, that's exactly it!

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