A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: Joystick

  1. #1
    Client Software Programmer AS3.0's Avatar
    Join Date
    Apr 2011
    Posts
    1,404

    Joystick

    Hi im doing an as3 version of my game so it can be on android for air 3.2 and im wondering how can i make some sort of joystickstyle button, a movieclip that can be dragged around, but is constrained to how far it can be dragged, and when released itl go back to its main point. Thanks

  2. #2
    Designer, Programmer, Musician angelhdz's Avatar
    Join Date
    Mar 2010
    Posts
    971
    var defX:Number;
    var defY:Number;

    joystick.addEventListener(MouseEvent.MOUSE_DOWN, joydrag);
    function joydrag(e:MouseEvent):void
    {
    defX = e.currentTarget.x;
    defY = e.currentTarget.y;
    e.currentTarget.startDrag();
    }

    joystick.addEventListener(MouseEvent.MOUSE_MOVE, moveJoy);
    function moveJoy(e:MouseEvent):void
    {
    if(e.currentTarget.hitTestObject(LEFT_BUTTON))
    {
    STOP(); // STOP is a function that limits the joystick from moving further
    }
    }
    joystick.addEventListener(MouseEvent.MOUSE_UP, stopdrag);
    function stopdrag(e:MouseEvent):void
    {
    e.currentTarget.stopDrag();
    //this resets the joystick to its default position
    e.currentTarget.x = defX;
    e.currentTarget.y = defY;
    }
    Something like that :P
    Already mastering AS3. It was so fun. Now into Javascript and PHP and all its libraries

  3. #3
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    Alloy, you also seem to be swapping to AS3 , maybe you should show some of what you have achieved with this project so far.

    But anyway, this is more along the lines of what you might require.
    Assuming that most joysticks move in a circular type way, this will get you started.

    i will attach a file too,
    PHP Code:
    import flash.display.Sprite;
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.MouseEvent;

    import fl.transitions.Tween;
    import fl.transitions.TweenEvent;
    import fl.transitions.easing.*;

    var 
    isDragging:Boolean false;
    var 
    span:int 50;

    var 
    joyStickReturn1:Tween;
    var 
    joyStickReturn2:Tween;

    var 
    theBase:jBase = new jBase();
    var 
    joyStick:jStick = new jStick();

    with(theBase)
    {
        
    joyStick.200;
        
    joyStick.200;
    }
    addChild(theBase);

    with (joyStick)
    {
        
    theBase.50;
        
    theBase.50;
        var 
    origX:Number x;
        var 
    origY:Number y;
        
    addEventListener(MouseEvent.MOUSE_DOWNenableJoyStick);
        
    addEventListener(MouseEvent.MOUSE_UPdisableJoyStick);
        
    // *** Uncomment below to enable drag out; comment out POINT2 ALSO if do so;
        //addEventListener(MouseEvent.MOUSE_OUT, disableJoyStick);// POINT1
    }
    addChild(joyStick);

    function 
    enableJoyStick(e:MouseEvent)
    {
        
    isDragging true;
        
    e.currentTarget.addEventListener(Event.ENTER_FRAMEdragStick);
        
    // *** Comment out below to prevent stage drag release;// Uncomment point1 IF DO SO;
        
    stage.addEventListener(MouseEvent.MOUSE_UPdisableJoyStick);// POINT 2
    }

    function 
    dragStick(e:Event)
    {
        if (
    isDragging == true)
        {
            var 
    angle:Number Math.atan2(mouseY origY,mouseX origX);
            var 
    distance:Number Math.sqrt(Math.pow(mouseX origX,2) + Math.pow(mouseY origY,2));
            if (
    Math.ceil(distance) >= span)
            {
                
    joyStick.origX + (Math.cos(angle) * span);
                
    joyStick.origY + (Math.sin(angle) * span);
            }
            else
            {
                
    joyStick.mouseX;
                
    joyStick.mouseY;
            }
        }
    }

    function 
    disableJoyStick(e:MouseEvent):void
    {
        
    isDragging false;
        
    doTweens();
    }

    function 
    doTweens(e:Event null):void
    {
        
    joyStick.removeEventListener(Event.ENTER_FRAMEdragStick);
        
    joyStickReturn1 = new Tween(joyStick,"x",null,joyStick.x,origX,1,false);
        
    joyStickReturn2 = new Tween(joyStick,"y",null,joyStick.y,origY,1,false);

    File:
    Last edited by fruitbeard; 03-23-2014 at 08:44 AM.

  4. #4
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    Slight update for you, with so called joystick arm attached, do with it as you will Alloy (or can).
    PHP Code:
    import flash.display.Sprite;
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.MouseEvent;

    import fl.transitions.Tween;
    import fl.transitions.TweenEvent;
    import fl.transitions.easing.*;

    import flash.filters.*;

    var 
    isDragging:Boolean false;
    var 
    dragSpan:int;
    var 
    origX:int;
    var 
    origY:int;

    var 
    joyStickReturn1:Tween;
    var 
    joyStickReturn2:Tween;

    var 
    theBase:jBase = new jBase();
    var 
    joyStick:jStick = new jStick();

    var 
    dropShadow:DropShadowFilter = new DropShadowFilter(2,45,0x000000,1,4,4,1,3);

    with (theBase)
    {
        
    100;
        
    100;
    }
    addChild(theBase);

    with (joyStick)
    {
        
    theBase.50;
        
    theBase.50;
        
    origX x;
        
    origY y;
        
    addEventListener(MouseEvent.MOUSE_DOWNenableJoyStick);
        
    addEventListener(MouseEvent.MOUSE_UPdisableJoyStick);
        
    // *** Uncomment below to enable drag out; COMMENT OUT POINT2 IF DO SO;
        // POINT 1
        //addEventListener(MouseEvent.MOUSE_OUT, disableJoyStick);
        
    filters = [dropShadow];
    }
    addChild(joyStick);

    function 
    enableJoyStick(e:MouseEvent)
    {
        
    isDragging true;
        
    e.currentTarget.addEventListener(Event.ENTER_FRAMEdragStick);
        
    // *** Comment out below to prevent stage drag release;// UNCOMMENT POINT 1 IF DO SO;
        // POINT 2
        
    stage.addEventListener(MouseEvent.MOUSE_UPdisableJoyStick);
    }

    function 
    dragStick(e:Event)
    {
        
    dragSpan theBase.width 2;
        if (
    isDragging == true)
        {
            var 
    angle:Number Math.atan2(mouseY origY,mouseX origX);
            var 
    distance:Number Math.sqrt(Math.pow(mouseX origX,2) + Math.pow(mouseY origY,2));
            if (
    Math.ceil(distance) >= dragSpan)
            {
                
    joyStick.origX + (Math.cos(angle) * dragSpan);
                
    joyStick.origY + (Math.sin(angle) * dragSpan);
            }
            else
            {
                
    joyStick.mouseX;
                
    joyStick.mouseY;
            }
            
    graphics.clear();
            
    graphics.lineStyle(250x0000001);
            
    graphics.moveTo(origX,origY);
            
    graphics.lineTo(joyStick.xjoyStick.y);
        }
    }

    function 
    disableJoyStick(e:MouseEvent):void
    {
        
    isDragging false;
        
    graphics.clear();
        
    doTweens();
    }

    function 
    doTweens(e:Event null):void
    {
        
    joyStick.removeEventListener(Event.ENTER_FRAMEdragStick);
        
    joyStickReturn1 = new Tween(joyStick,"x",null,joyStick.x,origX,1,false);
        
    joyStickReturn2 = new Tween(joyStick,"y",null,joyStick.y,origY,1,false);


  5. #5
    Client Software Programmer AS3.0's Avatar
    Join Date
    Apr 2011
    Posts
    1,404
    Thanks thatl do great for my android/ios version of my rpg

  6. #6
    Client Software Programmer AS3.0's Avatar
    Join Date
    Apr 2011
    Posts
    1,404
    Here is what I have achieved The first image will be pc/mac version and the on screen button image is my android version/ios version,
    its basically an 8bit mmorpg, with quests to do stats to train, people to fight, magic spells, archers, 8bit swords, bosses, massive maps

    http://i.imgur.com/jSan4ot.png
    http://i.imgur.com/PBCSByQ.png

    http://i.imgur.com/QVF9MYI.png
    http://i.imgur.com/54oGdyZ.png
    http://i.imgur.com/D8v74Ep.png

    which the servers and databases will be run on raspberry pis, set up as servers, this is an image to show you what raspberry pis are,
    http://i.imgur.com/7XUOYCc.png

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