Key.isDown to be used in a button
A Flash Developer Resource Site

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

Thread: Key.isDown to be used in a button

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    25

    Exclamation Key.isDown to be used in a button

    Hi! Please help! T_T

    What i want is to use buttons to move my character.. In my flash game i made a button, when clicked it will make my
    character move.. i don't need to use keyboard movements..

    This is my codings actionscript 3.0:


    import flash.events.Event;

    hero.gotoAndStop('still');
    var Key:KeyObject = new KeyObject(stage);

    stage.addEventListener(Event.ENTER_FRAME,onenter)

    function onenter(e:Event):void{

    if(Key.isDown(Key.RIGHT)){
    hero.x+=5;
    hero.scaleX=-1;
    hero.gotoAndStop('walking');
    }

    else if(Key.isDown(Key.LEFT)){
    hero.x-=5;
    hero.scaleX=1;
    hero.gotoAndStop('walking');
    }
    else hero.gotoAndStop('still');
    }

  2. #2
    :
    Join Date
    Dec 2002
    Posts
    3,511
    Maybe try this...

    Code:
    hero.gotoAndStop('still');
    hero.speed = 0;
    
    btnLeft.addEventListener(MouseEvent.MOUSE_DOWN, doLeft);
    btnRight.addEventListener(MouseEvent.MOUSE_DOWN, doRight);
    stage.addEventListener(MouseEvent.MOUSE_UP, doStill);
    stage.addEventListener(Event.ENTER_FRAME,onenter)
    
    
    function doLeft(e:Event):void {
            hero.speed = -5;
            hero.scaleX = 1;
            hero.gotoAndStop('walking');
    }
    function doRight(e:Event):void {
            hero.speed = 5;
            hero.scaleX = -1;
            hero.gotoAndStop('walking');
    }
    function doStill(e:Event):void {
            hero.speed = 0;
            hero.gotoAndStop('still');
    }
    function onenter(e:Event):void{
            hero.x += hero.speed;
    }

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    25
    OMG it works!! Thank you very much sir!

    I am so new in this programming things and i wan't to learn it. I still have one problem sir . I can't make my character jump. I still can't find the logic to make this work. It is still in Keyboard Event. Can you please translate it to the MouseEvent sir.. It works on the Keyboard event but I can't make it wirk on the jump_btn that i made..

    This is the codes of the jump:

    var dy:Number=0;
    var gravity:Number=1;
    var canjump:Boolean = false;

    stage.addEventListener(Event.ENTER_FRAME,onenter);


    function onenter(e:Event):void{

    dy+=gravity;
    if (hero.y>300){
    dy=0;
    canjump=true;
    }

    if (Key.isDown(Key.UP) && canjump){
    dy=-20;
    canjump = false;
    }

    hero.y+=dy;

  4. #4
    :
    Join Date
    Dec 2002
    Posts
    3,511
    Code:
    var dy:Number = 0;
    var gravity:Number = 1;
    var canjump:Boolean = false;
    
    hero.gotoAndStop('still');
    hero.speed = 0;
    hero.upval = 0;
    
    btnLeft.addEventListener(MouseEvent.MOUSE_DOWN, doLeft);
    btnRight.addEventListener(MouseEvent.MOUSE_DOWN, doRight);
    btnUp.addEventListener(MouseEvent.MOUSE_DOWN, doUp);
    stage.addEventListener(MouseEvent.MOUSE_UP, doStill);
    stage.addEventListener(Event.ENTER_FRAME,onenter);
    
    function doLeft(e:Event):void {
            hero.speed = -5;
            hero.scaleX = 1;
            hero.gotoAndStop('walking');
    }
    function doRight(e:Event):void {
            hero.speed = 5;
            hero.scaleX = -1;
            hero.gotoAndStop('walking');
    }
    function doUp(e:Event):void{
            hero.upval = -20;
    }
    function doStill(e:Event):void {
            hero.upval = 0;
            hero.speed = 0;
            hero.gotoAndStop('still');
    }
    function onenter(e:Event):void {
            hero.x +=  hero.speed;
            dy +=  gravity;
            if (hero.y > 300) {
                    dy = 0;
                    canjump = true;
            }
            if (canjump) {
                    dy = hero.upval;
                    canjump = false;
            }
            hero.y +=  dy;
    }

  5. #5
    Junior Member
    Join Date
    Jan 2013
    Posts
    25
    Thanks again sir! .

    I am making a side scroller game in android.
    My character is positioned in the center left of the stage so the user has more of a preview before they encounter an obstacle.
    The character can only move to the right, so when it moves to the right the background is scrolling to the left.
    I also have the jump button and move button.. I used the codes that you've translated to Mouse Event (above this post)


    I can't figure it out sir please help again T_T.

  6. #6
    :
    Join Date
    Dec 2002
    Posts
    3,511
    Perhaps this will give you some ideas...
    Code:
    // added two background movie clips to the stage,
    // instance names bgA and bgB, both must be wider than stage
    
    var dy:Number = 0;
    var gravity:Number = 1;
    var canjump:Boolean = false;
    var minObj:int = 2;
    var maxObj:int = 5;
    var objs:Sprite = new Sprite();
    var hurdle:Hurdle;
    var healthAmt:int = 10;
    var maxHealth:int = health.width;
    
    hero.gotoAndStop('still');
    hero.upval = 0;
    bgA.speed = 5;
    bgA.x = 0;
    bgB.x = bgA.x + bgA.width;
    addObj(bgB);
    
    btnPause.addEventListener(MouseEvent.MOUSE_DOWN, doPause);
    btnJump.addEventListener(MouseEvent.MOUSE_DOWN, doJump);
    stage.addEventListener(MouseEvent.MOUSE_UP, doWalk);
    stage.addEventListener(Event.ENTER_FRAME,onenter);
    
    function doPause(e:Event):void {
            bgA.speed = 0;
            hero.scaleX = -1;
            hero.gotoAndStop('still');
    }
    function doJump(e:Event):void {
            hero.upval = -20;
    }
    function doWalk(e:Event):void {
            hero.upval = 0;
            bgA.speed = 5;
            hero.gotoAndStop('walking');
    }
    function onenter(e:Event):void {
            bgA.x -=  bgA.speed;
            bgB.x -=  bgA.speed;
            if (bgA.x <  -  bgA.width) {
                    bgA.x = bgB.x + bgB.width;
                    addObj(bgA);
            }
            if (bgB.x <  -  bgB.width) {
                    bgB.x = bgA.x + bgA.width;
                    addObj(bgB);
            }
            dy +=  gravity;
            if (hero.y > 300) {
                    dy = 0;
                    canjump = true;
            }
            if (canjump) {
                    dy = hero.upval;
                    canjump = false;
            }
            hero.y +=  dy;
            if (hero.hitTestObject(bgA)) {
                    checkHit(bgA);
            }
            if (hero.hitTestObject(bgB)) {
                    checkHit(bgB);
            }
    }
    function checkHit($mc:MovieClip):void {
            objs = $mc.getChildByName($mc.name) as Sprite;
            if (objs) {
                    for (var i:int; i < objs.numChildren; i++) {
                            hurdle = (objs.getChildAt(i)) as Hurdle;
                            if (! hurdle.gotHit) {
                                    if (hero.hitTestObject(hurdle)) {
                                            hurdle.gotHit = true;
                                            health.width -= (maxHealth / healthAmt);
                                            if (health.width <= 0){
                                                    trace("game over");
                                                    stage.removeEventListener(Event.ENTER_FRAME,onenter);
                                            }
                                    }
                            }
                    }
            }
    }
    function addObj($mc:MovieClip):void {
            objs = $mc.getChildByName($mc.name) as Sprite;
            if (objs) {
                    $mc.removeChild(objs);
            }
            objs = new Sprite();
            objs.name = $mc.name;
            var numObj:int = randomRange(minObj,maxObj);
            for (var i:int = 0; i < numObj; i++) {
                    hurdle = new Hurdle();
                    hurdle.x = randomRange(hurdle.width,$mc.width - hurdle.width);
                    hurdle.y = 300 - hurdle.height;
                    objs.addChild(hurdle);
            }
            $mc.addChild(objs);
    }
    function randomRange(min:Number, max:Number):Number {
            return (min + Math.random() * (max - min));
    }
    Last edited by dawsonk; 01-15-2013 at 11:37 PM.

  7. #7
    Junior Member
    Join Date
    Jan 2013
    Posts
    25
    Ohhhh men I can't understand the codes. What should I do, I did make two wide backgrounds, after that I'm lost . So noob at programming

  8. #8
    :
    Join Date
    Dec 2002
    Posts
    3,511
    Also need to make a hurdle movie clip, in the library give it export linkage name of "Hurdle".
    Make a health bar movie clip on the stage with instance name of "health".
    And change instance name of buttons to match the code.

  9. #9
    Junior Member
    Join Date
    Jan 2013
    Posts
    25
    I did what you told me. I made two movie clips with instance name of "health" and "Hurdle"(Hurdle is also the name at the Library)
    Then I changed the name of the codes to jump_btn and pause_btn because that is the instance name of my two buttons.

    When i test movie, it has errors.

    Scene 1, Layer 'as3', Frame 1, Line 10 1046: Type was not found or was not a compile-time constant: Hurdle.


    Scene 1, Layer 'as3', Frame 1, Line 71 1067: Implicit coercion of a value of type flash.display:MovieClip to an unrelated type Class.

    Scene 1, Layer 'as3', Frame 1, Line 94 1180: Call to a possibly undefined method Hurdle.

  10. #10
    Junior Member
    Join Date
    Jan 2013
    Posts
    25
    Ahmm hey. Do yoy know how to make obstacles and a health bar for the runner game?.
    I can't figure it out sir T_T. Please Help

  11. #11
    Junior Member
    Join Date
    Jan 2013
    Posts
    25
    I got it working already.
    But there is a problem the two background are overlapping each other, can I just use one background because I have a background already which i made.

  12. #12
    Junior Member
    Join Date
    Jan 2013
    Posts
    25
    Uhmmm sir dawsonk. When I test movie it at first the two background are connected and then after that when the backgrouds show up again it is not already connected, there is a gap in between bgA and bgB. How can I fix that? please sir help.

  13. #13
    :
    Join Date
    Dec 2002
    Posts
    3,511
    Check your code for mistakes, it shouldn't allow the backgrounds to overlap.

  14. #14
    Junior Member
    Join Date
    Jan 2013
    Posts
    25
    The two background are now not overlapping but there is a gap in between bgA and bgB. At first the two backgrounds are connected then after that when it loops the two backgrounds have a gap.
    Last edited by juliuz0001; 01-18-2013 at 09:35 AM.

  15. #15
    Junior Member
    Join Date
    Jan 2013
    Posts
    25
    Please sir dawsonk please help me T_T

  16. #16
    Junior Member
    Join Date
    Jan 2013
    Posts
    25
    Please see my flash project at http://www.filedropper.com/runner

    Just hold down the right_btn to move then you will get to the point where the obstacle are showing up.

    I just use one background. I removed the bgB codes. The problem is i don't know the conditionals to use. It's on function oneneter ad addObj (bgA);
    I need to put conditionals on addObj(bgA);
    When the obstacles appear it never stops and is very fast. I don't know what conditionals to use.

    Please help me to get the conditionals sir!

  17. #17
    :
    Join Date
    Dec 2002
    Posts
    3,511
    Sorry I only have CS5 and can't open your file.

  18. #18
    Junior Member
    Join Date
    Jan 2013
    Posts
    25
    I'll transfer it to cs5 sir. Wait

  19. #19
    Junior Member
    Join Date
    Jan 2013
    Posts
    25
    http://www.filedropper.com/eggrunner

    Just hold down the right_btn to move then you will get to the point where the obstacle are showing up.

    The problem is i don't know the conditionals to use. It's on function oneneter addObj (bgA);
    I need to put conditionals on addObj(bgA);
    When the obstacles appear it never stops and is very fast. I don't know what conditionals to use.
    The bgB is for the sky background - The codes on bgB is just movement.

    Please help me sir!

  20. #20
    :
    Join Date
    Dec 2002
    Posts
    3,511
    bgA, bgB, and health movie clip must have registration at top left (not in middle)
    Code:
    var SWIDTH = 480;
    var dy:Number = 0;
    var gravity:Number = 1;
    var canjump:Boolean = false;
    var objs:Sprite;
    var hurdle:Hurdle;
    var healthAmt:int = 10;
    var maxHealth:int = health.width;
    // set number of hurdles to show
    var numObj:int = 50;
     
    function init():void {
            hero.gotoAndStop('still');
            hero.speed = 0;
            hero.upval = 0;
            bgA.speed = bgB.speed = 0;
            bgA.x = bgB.x = 0;
            addObjs();
            right_btn.addEventListener(MouseEvent.MOUSE_DOWN, doRight);
            jump_btn.addEventListener(MouseEvent.MOUSE_DOWN, doUp);
            stage.addEventListener(MouseEvent.MOUSE_UP, doStill);
            stage.addEventListener(Event.ENTER_FRAME,onenter);
    }
    function doRight(e:Event):void {
            hero.speed = 0;
            bgA.speed = -4;
            bgB.speed = -2.5;
            hero.gotoAndStop('walking');
    }
    function doUp(e:Event):void {
            hero.upval = -10;
    }
    function doStill(e:Event):void {
            hero.upval = 0;
            hero.speed = 0;
            bgA.speed = 0;
            bgB.speed = 0;
            hero.gotoAndStop('still');
    }
    function onenter(e:Event):void {
            bgA.x +=  bgA.speed;
            bgB.x +=  bgB.speed;
            if (bgA.x <= 0 - (bgA.width - SWIDTH) ) {
                    bgA.x -=  bgA.speed;
                    gameOver();
            }
            hero.x +=  hero.speed;
            dy +=  gravity;
            if (hero.y > 220) {
                    dy = 0;
                    canjump = true;
            }
            if (canjump) {
                    dy = hero.upval;
                    canjump = false;
            }
            hero.y +=  dy;
            for (var i:int; i < objs.numChildren; i++) {
                    hurdle = (objs.getChildAt(i)) as Hurdle;
                    if (! hurdle.gotHit) {
                            if (hero.hitTestObject(hurdle)) {
                                    hurdle.gotHit = true;
                                    health.width -= (maxHealth / healthAmt);
                                    if (health.width <= 0) {
                                            gameOver();
                                    }
                            }
                    }
            }
    }
    function gameOver():void {
            trace("game over");
            hero.gotoAndStop('still');
            stage.removeEventListener(Event.ENTER_FRAME,onenter);
            right_btn.removeEventListener(MouseEvent.MOUSE_DOWN, doRight);
            jump_btn.removeEventListener(MouseEvent.MOUSE_DOWN, doUp);
            stage.removeEventListener(MouseEvent.MOUSE_UP, doStill);
    }
    function addObjs():void {
            if (objs) {
                    bgA.removeChild(objs);
            }
            objs = new Sprite();
            for (var i:int = 0; i < numObj; i++) {
                    hurdle = new Hurdle();
                    hurdle.x = randomRange(SWIDTH,bgA.width - SWIDTH);
                    hurdle.y = 280 - hurdle.height;
                    objs.addChild(hurdle);
            }
            bgA.addChild(objs);
    }
    function randomRange(min:Number, max:Number):int {
            return (min + Math.random() * (max - min))>>0;
    }
    init();

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