A Flash Developer Resource Site

Results 1 to 11 of 11

Thread: how to get ball to move left/right and shoot paint

  1. #1
    Member
    Join Date
    Jun 2004
    Posts
    44

    how to get ball to move left/right and shoot paint

    I am adapting code to make a paintball game that will run on a handheld device. I have trouble figuring out how to get the yellow ball to move left and right with keypresses AND shoot paint. Do I need to change the movie to a button and somehow put the movie inside? Do you know how to do this? The file is attached. Thanks for your help! Brigit
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    Apr 2001
    Location
    Akron OH, USA
    Posts
    4,841
    please review the forum guidelines for this forum. We need to know more information about the version of Flash you are developing for.

    We also need to know what you have tried that does not work.

    For example are you trying to get the yellow ball to move and shoot paint from one key press or are you moving the ball with one set of keys, like left/right arrows and shooting paint with the enter key.

    The more specific you are with your questions, the more likely someone will be able to help you.

    Here is a post on how to move objects around on the screen using Flash Lite 1.1 ActionScript.
    http://board.flashkit.com/board/showthread.php?t=759330

  3. #3
    Member
    Join Date
    Jun 2004
    Posts
    44
    Thanks for responding. I'm using Flash8, exporting as a Flash Lite 2.1 file.

    I have been thinking I'd make an off-screen button to hold the keypress events. I did that in another game and that worked. But the thing is that I want the keypress events to move the ball from the left to the right AND have the ball shoot paint at the balls above. That's what's in the animation presently. Can I put all of that in an off-screen button?

    I think I did make an off-screen button and I had this working, but for the shooting paint. I think I even had left, right, up, and down working. But no flying paint.


    I want the left right buttons to move the ball, and the up button to shoot the paint.

    So I figure I make the yellow ball a button. And then I have an off-screen button with the code to move the ball left and right with the arrow keys. Do I just put the up info with the shooting paint in that same off-screen button? I think I tried that and it didn't work.... There's all the code on the timeline's frame 2 -- I keep trying to just cut and paste pieces of that in to these new buttons (the yellow onscreen ball and an off-screen button).

    Thanks for your help.

  4. #4
    Registered User
    Join Date
    Apr 2001
    Location
    Akron OH, USA
    Posts
    4,841
    Flash Lite will trap events for the button with focus. Consequently, it is simplest to have one button to trap all events.

    I would recommend, one off screen button to control all events in your game. Put the left/right up/down and enter key events all in the same off screen button.

    Do not make the paint ball a button and do not use multiple buttons or else your code will not behave as you expect.

  5. #5
    Member
    Join Date
    Jun 2004
    Posts
    44
    Thanks. It helps to have the clear direction of not making the paint ball a button, and one off screen button to controll all KeyPress events in the game.

    I tried several different things and haven't quite gotten it yet. I had the ball going left, right, up and down with keypress events. But I haven't figured out how to get the paint to splat out with the keypress Up event. Can you give any more advice? I'd appreciate it.

  6. #6
    Member
    Join Date
    Jun 2004
    Posts
    44
    The ball movement (left right up down) was all inside one off screen button...

  7. #7
    Registered User
    Join Date
    Apr 2001
    Location
    Akron OH, USA
    Posts
    4,841
    there are many ways you could do this. Perhaps the simplest is to have a movie clip with the paint splashing out, in a frame within the ball movieclip, and just play that movieclip from the enter key event. This child movie clip should have a stop action in the first frame so it will only play when the user presses the enter key.

  8. #8
    Member
    Join Date
    Jun 2004
    Posts
    44

    trade art for actionscript

    If you are willing to fix the code I would make you a piece of art or two for your game in return....

    I still am confused about how to get the ball to move with the left right arrow keys and how to get the paint to shoot up from the up arrow key. It plays the way I want it to when viewed on a PC. When viewed on a handheld the paint no longer shoots.

    I put another version of the file here, in case you are willing to do a trade....
    Attached Files Attached Files

  9. #9
    Member
    Join Date
    Jun 2004
    Posts
    44

    Trade art for actionscript...

    Please see earlier entries for more information. Thanks!

  10. #10
    Registered User
    Join Date
    Apr 2001
    Location
    Akron OH, USA
    Posts
    4,841
    unfortunately, I do not have time to review your FLA. If you can provide specific questions regarding your problem then someone in the forum will hopefully be able to answer them. Good luck with your game.

  11. #11
    Member
    Join Date
    Jun 2004
    Posts
    44

    keypress functionality

    Where I am getting lost is understanding what lines of actionscript to move from frame 2 of the .fla (that's where almost all the actionscript is) into an invisible button that I have on the same point of the timeline, in a buttons layer. I can take KeyPress actionscript from another game I made and put it in the invisible button, and it sort of works, but I'm causing a conflict because there is Key.isDown actionscript on the main frame too. I am hoping there is something as easy as isolating part of the script and putting it in the invisible button. I have tried many different things and have yet to get the application working correctly on the handheld device. It runs fine on the PC. Also, yesterday I upgraded to CS3. This is in ActionScript 2.0.

    This is the actionscript:

    function bullets():Void {
    //
    // Function to animate bullet...
    this.onEnterFrame = function() {
    if (fired) {
    this._visible = true;
    this.bulletY = this.bulletY-speed*3;
    this._y = this.bulletY;
    if ((this.bulletY<gSCREEN_TOP) || (fired == false)) {
    fired = false;
    this.bulletY = gSCREEN_BOTTOM-gSHIP_HEIGHT;
    }
    } else {
    this._visible = false;
    }
    };
    this.bulletX = 0;
    this.bulletY = 0;
    }
    /************************************************** *********

    FUNCTIONS AND EVENTS FOR 'SHIP'

    */
    function starShip():Void {
    //
    // Ship sprite logic...
    //
    this.onEnterFrame = function() {
    if (Key.isDown(this.leftArrow) && (this.shipX>gSCREEN_LEFT)) {
    this.shipX -= speed;
    }
    if (Key.isDown(this.rightArrow) && (this.shipX<gSCREEN_RIGHT)) {
    this.shipX += speed;
    }
    if (Key.isDown(this.upArrow) && (!fired)) {
    fired = true;
    bullet.bulletX = bullet._x=this.shipX;
    bullet.bulletY = bullet._y=this.shipY-gSHIP_HEIGHT;
    }
    if (shipDead) {
    this.gotoAndPlay("explodeMe");
    delete this.onEnterFrame;
    }
    // ship movement
    this._x = this.shipX;
    };
    //
    // Initialize ship on first run...
    this.leftArrow = 37;
    this.rightArrow = 39;
    this.upArrow = 38;
    this.shipX = (gSCREEN_RIGHT-gSCREEN_LEFT)/2;
    this.shipY = gSCREEN_BOTTOM;
    this._x = this.shipX;
    this._y = this.shipY;
    this.gotoAndStop(1);
    }
    /************************************************** *********

    FUNCTIONS AND EVENTS FOR 'ALIEN'

    */
    function spaceAlien():Void {
    //
    // Initialize alien...
    this.alienSpawn = function() {
    // cache this alien now it is alive
    this.cacheAsBitmap = true;
    this.alienX = this._x=this.startX;
    this.alienY = this._y=this.startY;
    this.targetX = Math.random()*(gSCREEN_RIGHT-gSCREEN_LEFT)/2;
    this.targetY = 100;
    this.alienAdvance = 4;
    };
    //
    // main living alien logic...
    this.alienBrain = function() {
    this.distX = this.targetX-this.alienX;
    this.distY = this.targetY-this.alienY;
    // Apply inertia equation
    this.alienX = this.alienX+(this.distX/accel);
    this.alienY = this.alienY+(this.distY/accel);
    if ((this.distX<15) && (this.distY<15)) {
    this.alienAdvance -= 0.05;
    this.alienInitialize();
    }
    this._x = this.alienX;
    this._y = this.alienY;
    // check for collisions
    if (this.hitTest(bullet) && fired) {
    // disable caching on this alien while it explodes
    this.cacheAsBitmap = false;
    fired = false;
    this.gotoAndPlay("explodeMe");
    this.alienHit();
    }
    if (this.hitTest(ship)) {
    shipDead = true;
    this.targetY = 100;
    }
    };
    //
    // main dead alien logic...
    this.alienReincarnate = function() {
    if (Math.random()<0.005) {
    this.gotoAndStop("showMe");
    this.alienSpawn();
    this.onEnterFrame = function() {
    this.alienBrain();
    };
    }
    };
    //
    // function to set alien's current destination...
    this.alienInitialize = function() {
    if (skill<Math.random()) {
    this.targetX = Math.random()*(gSCREEN_RIGHT-gSCREEN_LEFT);
    this.targetY = gSCREEN_TOP+Math.random()*(gSCREEN_BOTTOM/this.alienAdvance);
    } else {
    this.targetX = ship._x+((Math.random()*100)-50);
    this.targetY = ship._y-5;
    }
    if (targetY>gSCREEN_BOTTOM) {
    targetY = gSCREEN_BOTTOM+10;
    }
    };
    //
    // function to handle alien being hit...
    this.alienHit = function() {
    score += level*10;
    score_txt.text = score;
    this.onEnterFrame = this.alienReincarnate;
    };
    //
    // Spawn a living alien on first run...
    this.startX = (gSCREEN_RIGHT-gSCREEN_LEFT)/2;
    this.startY = gSCREEN_TOP-500;
    this.alienSpawn();
    this.onEnterFrame = this.alienBrain;
    }
    function makeAlien(quantity:Number):Void {
    var i, clip;
    for (i=0; i<quantity; i++) {
    clip = this.attachMovie("alien", "alien"+i, i+200);
    spaceAlien.apply(clip);
    }
    }
    function gameRules():Void {
    // game rules...
    if (score>(level*200)) {
    skill = skill*2;
    accel = accel/1.5;
    level++;
    level_txt.text = level;
    }
    if (shipDead == true) {
    //shipDead = false;
    delete this.onEnterFrame;
    gotoAndPlay("gameEnd");
    }
    }
    function initGo():Void {
    // reset variables for this turn
    speed = 10;
    skill = 0.04;
    accel = 20;
    score = 0;
    level = 1;
    fired = false;
    shipDead = false;
    // clear score and level text
    score_txt.text = score;
    level_txt.text = level;
    }
    /************************************************** *********

    MAIN PROGRAM

    */
    // set up screen extents
    _global.gSCREEN_LEFT = 20;
    _global.gSCREEN_RIGHT = 220;
    _global.gSCREEN_TOP = 20;
    _global.gSCREEN_BOTTOM = 300;
    _global.gSHIP_HEIGHT = 36;
    //
    // event handler for invisible button
    invisible_btn.onRelease = function() {
    this.enabled = false;
    initGo();
    makeStar(10);
    makeAlien(6);
    bullets.apply(bullet);
    starShip.apply(ship);
    gotoAndStop("gameStart");
    };
    // set up rules
    this.onEnterFrame = gameRules;
    // move timelines...
    ship.stop();
    gotoAndStop("attractor");

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