Collision with an object replacing the mouse
A Flash Developer Resource Site

Results 1 to 12 of 12

Thread: Collision with an object replacing the mouse

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    24

    Exclamation Collision with an object replacing the mouse

    Hi,
    I am a beginner to Actionscript 3
    I am creating a simple game (for practice purpose)

    here is my code:

    Code:
    	
    
    stop();
    
    var lvl1:MovieClip = new Linelvl1();
    lvl1.x = 400;
    lvl1.y = 240;
    addChild(lvl1);
    lvl1.alpha = 0;
    
    var mouse:MovieClip = new Ball();
    mouse.x = 400;
    mouse.y = 240;
    addChild(mouse);
    
    addEventListener(Event.ENTER_FRAME, onStart);
    
    function onStart(evt:Event) {
                    if (lvl1.hitTestPoint(mouseX, mouseY, true)) {
    		removeEventListener(Event.ENTER_FRAME, onStart);
    		removeChild(lvl1);
    		gotoAndStop("main");
    		}
    	}
    	mouse.x = mouseX;
    	mouse.y = mouseY;
    	Mouse.hide();
    }
    So now, my question is this.
    As you've noticed I created a "Ball" that replaces the mouse and I've hid the mouse. Whenever the mouse or the "Ball" touches "lvl1" I want it to go to "main". But it doesn't work
    I even changed hitTestPoint to hitTestObject but....I don't know why it doesn't work

    Any help...?

  2. #2
    Dignitary rynoe's Avatar
    Join Date
    Jan 2003
    Location
    Earth
    Posts
    756
    one to many }

    PHP Code:
    stop();

    var 
    lvl1:MovieClip = new Linelvl1();
    lvl1.400;
    lvl1.240;
    addChild(lvl1);
    lvl1.alpha 0;

    var 
    mouse:MovieClip = new Ball();
    mouse.400;
    mouse.240;
    addChild(mouse);

    addEventListener(Event.ENTER_FRAMEonStart);

    function 
    onStart(evt:Event) {
              if (
    lvl1.hitTestPoint(mouseXmouseYtrue)) {
            
    removeEventListener(Event.ENTER_FRAMEonStart);
            
    removeChild(lvl1);
            
    gotoAndStop("main");
            }
        
    mouse.mouseX;
        
    mouse.mouseY;
        
    Mouse.hide();

    worked for me

  3. #3
    Junior Member
    Join Date
    Jul 2012
    Posts
    24
    Thank you
    But it still doesn't work for me

    Maybe it's because I didn't put the whole code
    That's the whole code

    PHP Code:
    stop();

    var 
    lvl1:MovieClip = new Linelvl1();
    lvl1.400;
    lvl1.240;
    addChild(lvl1);
    lvl1.alpha 0;

    addEventListener(Event.ENTER_FRAMEonStart);

    function 
    onStart(evt:Event) {
        
        var 
    myTimer:Timer = new Timer(10001);
        
    myTimer.addEventListener(TimerEvent.TIMERtimerListener);
        
            function 
    timerListener (evt:TimerEvent) {
                if (
    lvl1.alpha .75) {
            
    lvl1.alpha += .05;
        }
            }
        
    myTimer.start();
        if (
    lvl1.alpha == .75) {
        if (
    lvl1.hitTestPoint(mouseXmouseYtrue)) {
            
    removeEventListener(Event.ENTER_FRAMEonStart);
            
    removeChild(lvl1);
            
    gotoAndStop("main");
            }
        }

    With this code as soon as my mouse touches "lvl1" it works
    but I want to hide the mouse and replace the mouse with another object. as soon as THAT object hits "lvl1" I wanted to do a specific action.
    Now what shall I add?
    shall I use hitTestObject or hitTestPoint?

  4. #4
    Junior Member
    Join Date
    Jul 2012
    Posts
    24
    Actually here's the complete explanation

    This is what it looks like



    The black ball can be dragged. I want the ball to be able to travel between the hole. But if it touches the black coloured area, it has to go and play "main"

    And this is my code

    Actionscript Code:
    stop();

    var lvl1:MovieClip = new Linelvl1();
    lvl1.x = 400;
    lvl1.y = 240;
    addChild(lvl1);
    lvl1.alpha = 0;

    var ball:MovieClip = new Ball();
    ball.x = 80;
    ball.y = 380;
    addChild(ball);

    addEventListener(Event.ENTER_FRAME, onStart);

    function onStart(evt:Event) {
       
        var myTimer:Timer = new Timer(1000, 1);
        myTimer.addEventListener(TimerEvent.TIMER, timerListener);
       
            function timerListener (evt:TimerEvent) {
                if (lvl1.alpha < .75) {
            lvl1.alpha += .05;
        }
            }
        myTimer.start();
        ball.addEventListener(MouseEvent.MOUSE_DOWN, onDrag);
        ball.addEventListener(MouseEvent.MOUSE_UP, onStopDrag);
        function onDrag(evt:MouseEvent) {
            ball.startDrag(true);
        }
        function onStopDrag(evt:MouseEvent) {
            ball.stopDrag();
        }
       
        if (lvl1.alpha == .75) {
        if (lvl1.hitTestObject(ball)) {
            removeEventListener(Event.ENTER_FRAME, onStart);
            removeChild(lvl1);
            removeChild(ball);
            gotoAndStop("main");
            ball.removeEventListener(MouseEvent.MOUSE_DOWN, onDrag);
            ball.removeEventListener(MouseEvent.MOUSE_UP, onStopDrag);
            }
        }
    }

  5. #5
    Dignitary rynoe's Avatar
    Join Date
    Jan 2003
    Location
    Earth
    Posts
    756
    Clips are always squares for hitTest objects so you kinda have to make 3 separate walls unless you wanna use some of the fancier hitTest class files out there.
    I'm not sure of the need for your objects to be class files in the library either. If you can, you should consider just drawing the object and convert to symbols. It would make the code rather simple:

    PHP Code:
    import flash.events.MouseEvent;
    import flash.events.Event;

    ball.addEventListener(MouseEvent.MOUSE_DOWNstartDragging);
    ball.addEventListener(MouseEvent.MOUSE_UPstopDragging);
    ball.addEventListener(Event.ENTER_FRAMEhitWalls);

    function 
    hitWalls(e:Event):void{
        if(
    e.currentTarget.hitTestObject(wall1)||e.currentTarget.hitTestObject(wall2)||e.currentTarget.hitTestObject(wall3)){
            
    e.currentTarget.stopDrag();
            
    e.currentTarget.x=28;
            
    e.currentTarget.303;
        }
    }
    function 
    stopDragging(m:MouseEvent):void{
        
    m.currentTarget.stopDrag();
    }
    function 
    startDragging(m:MouseEvent):void{
        
    m.currentTarget.startDrag();


  6. #6
    Junior Member
    Join Date
    Jul 2012
    Posts
    24
    Quote Originally Posted by rynoe View Post
    Clips are always squares for hitTest objects so you kinda have to make 3 separate walls unless you wanna use some of the fancier hitTest class files out there.
    I'm not sure of the need for your objects to be class files in the library either. If you can, you should consider just drawing the object and convert to symbols. It would make the code rather simple:

    PHP Code:
    import flash.events.MouseEvent;
    import flash.events.Event;

    ball.addEventListener(MouseEvent.MOUSE_DOWNstartDragging);
    ball.addEventListener(MouseEvent.MOUSE_UPstopDragging);
    ball.addEventListener(Event.ENTER_FRAMEhitWalls);

    function 
    hitWalls(e:Event):void{
        if(
    e.currentTarget.hitTestObject(wall1)||e.currentTarget.hitTestObject(wall2)||e.currentTarget.hitTestObject(wall3)){
            
    e.currentTarget.stopDrag();
            
    e.currentTarget.x=28;
            
    e.currentTarget.303;
        }
    }
    function 
    stopDragging(m:MouseEvent):void{
        
    m.currentTarget.stopDrag();
    }
    function 
    startDragging(m:MouseEvent):void{
        
    m.currentTarget.startDrag();

    Thank you for your help
    I'm really trying to understand what you're saying but I can't

    1. My wall is only one as you see it in the picture. How do you want me to create 3 walls

    2. what are the x = 28 and y = 303 for??
    \
    thank you

  7. #7
    Junior Member
    Join Date
    Jul 2012
    Posts
    24
    I'm sorry please ignore my last post
    I got it

    Thank you

    But now this is level 1
    what if in future levels I would wanna put a let's say diagonal wall....??
    then when I convert it to a symbol it'll be square.... (

  8. #8
    Is the best Alloy Bacon's Avatar
    Join Date
    Apr 2011
    Posts
    395
    Actually it is possible to make pixel perfect hit tests if you use .as files check this free source code that has pixel perfect collisions and a free download: http://www.freeactionscript.com/cate...ion-detection/

  9. #9
    Dignitary rynoe's Avatar
    Join Date
    Jan 2003
    Location
    Earth
    Posts
    756
    You can also download the latest kit here.

    This is one of the 'fancier' class files I mentioned in earlier post. If you are just starting AS3 this may seem a bit overwhelming though.
    Last edited by rynoe; 07-18-2012 at 11:12 AM. Reason: Explosive diarrhea

  10. #10
    Junior Member
    Join Date
    Jul 2012
    Posts
    24
    Quote Originally Posted by rynoe View Post
    You can also download the latest kit here.

    This is one of the 'fancier' class files I mentioned in earlier post. If you are just starting AS3 this may seem a bit overwhelming though.
    I've read the book ActionScript 3 for Beginners
    It's an amazing book and they suggested this for pixel perfect also.
    But no one ever explained how to use it in your own project

  11. #11
    Junior Member
    Join Date
    Jul 2012
    Posts
    24
    For example I want exactly something like this
    but this is in AS2
    I want it in AS3

    http://gskinner.com/blog/archives/20...8_shape_b.html

  12. #12
    Junior Member
    Join Date
    Jul 2012
    Posts
    24
    ??

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

Poll by Flashkit.com