Home Tutorials Forums Articles Blogs Movies Library Employment Press

Go Back   ActionScript.org Forums > General > Gaming and Game Development

Reply
 
Thread Tools Rate Thread Display Modes
Old 06-11-2011, 02:06 PM   #1
juuri
Registered User
 
Join Date: Jun 2011
Posts: 2
Question [AS3] Help with basic game code

I'm extremely new to Actionscript and am trying to make a basic bouncing ball game like the one in the DevNote Tutorial 'Intro to Flash Game Programming' on Youtube

However, I want the ball to enter the stage at the beginning of the game.

I would also like to add another dynamic text field that counts the rounds of the game (i.e. when a person drops the ball the first time, the Rounds counter goes to 1 when the drop it a second time it goes to 2 etc.) and at the end of the third round I want the game to end.

If anyone could post some script that would do what i have outlined above I would be very appreciative!

thanks in advance!
juuri is offline   Reply With Quote
Old 06-11-2011, 06:40 PM   #2
benjio21
Senior Member
 
Join Date: Jan 2011
Location: uk
Posts: 502
Send a message via Skype™ to benjio21
Default

Why don't you have a go yourself and then ask us for help. The pros and forum users are not here to do it for you. That's what tutorials are for.

Ben
__________________
as3 is like marmite ("you either hate it or love it");
benjio21 is offline   Reply With Quote
Old 06-11-2011, 07:01 PM   #3
MrAbrman
Registered User
 
Join Date: Jun 2011
Posts: 10
Default

This is the code I'd use. on stage I would have text field with instance name "rounds_txt" and a button with a instance name "add_btn" when the ball falls it should be a hittest with the area below the stage or a comparison between the ball position on y and stage height but for example I just use a button that would be that event ingame.

1st I create a variabile storing my round number,
Then I created the EventListener for the button, you should use
ActionScript Code:
addEventListener(Event.ENTER_FRAME, onEnterFrame); function onEnterFrame (e:Event) :void { if (ball.y >= stage.y){         ball.x = 100 //the x position where the ball should start to avoid  adding more rounds than one per fall         ball.y = 100 //the y position where the ball should start         rounds_num += 1     if (rounds_num <= 3){     trace("3");     } }
In the game :P
and at the end there is a EnterFrame Event which puts our variabile that stores the round number in the text bux for the user to see it :P

ActionScript Code:
import flash.events.MouseEvent; import flash.events.Event; var rounds_num:Number = 0 add_btn.addEventListener(MouseEvent.CLICK, oneup) function oneup(e:MouseEvent) :void {     rounds_num += 1 //Could use rounds_num = rounds_num + 1 also     if (rounds_num >= 3){ //>= means 3 or more, == would be exactly 3     trace("3"); } } addEventListener(Event.ENTER_FRAME, onEnterFrame); function onEnterFrame (e:Event) :void { rounds_txt.text = String(rounds_num); }

Hope I was helpful

Last edited by MrAbrman; 06-11-2011 at 07:10 PM. Reason: error in the code
MrAbrman is offline   Reply With Quote
Old 06-11-2011, 07:03 PM   #4
MrAbrman
Registered User
 
Join Date: Jun 2011
Posts: 10
Default

And Ben I actually helped him :PP I get practice, cause I'm new to AS and really need it and I get posts so I will be able to add links xD
Anyways if you're good with AS3 and you feel like helping me out please check showthread.php3?t=261476
MrAbrman is offline   Reply With Quote
Old 06-11-2011, 07:46 PM   #5
benjio21
Senior Member
 
Join Date: Jan 2011
Location: uk
Posts: 502
Send a message via Skype™ to benjio21
Default

If you want to make it more realistic you should add gravity & x&y velocity
Ben
Quote:
Originally Posted by MrAbrman View Post
This is the code I'd use. on stage I would have text field with instance name "rounds_txt" and a button with a instance name "add_btn" when the ball falls it should be a hittest with the area below the stage or a comparison between the ball position on y and stage height but for example I just use a button that would be that event ingame.

1st I create a variabile storing my round number,
Then I created the EventListener for the button, you should use
ActionScript Code:
addEventListener(Event.ENTER_FRAME, onEnterFrame); function onEnterFrame (e:Event) :void { if (ball.y >= stage.y){         ball.x = 100 //the x position where the ball should start to avoid  adding more rounds than one per fall         ball.y = 100 //the y position where the ball should start         rounds_num += 1     if (rounds_num <= 3){     trace("3");     } }
In the game :P
and at the end there is a EnterFrame Event which puts our variabile that stores the round number in the text bux for the user to see it :P

ActionScript Code:
import flash.events.MouseEvent; import flash.events.Event; var rounds_num:Number = 0 add_btn.addEventListener(MouseEvent.CLICK, oneup) function oneup(e:MouseEvent) :void {     rounds_num += 1 //Could use rounds_num = rounds_num + 1 also     if (rounds_num >= 3){ //>= means 3 or more, == would be exactly 3     trace("3"); } } addEventListener(Event.ENTER_FRAME, onEnterFrame); function onEnterFrame (e:Event) :void { rounds_txt.text = String(rounds_num); }

Hope I was helpful
__________________
as3 is like marmite ("you either hate it or love it");
benjio21 is offline   Reply With Quote
Old 06-12-2011, 12:25 AM   #6
juuri
Registered User
 
Join Date: Jun 2011
Posts: 2
Default

thanks so much for the code! I'm not sure if it will work with the code i've already put in though... this is what i had so far:

ActionScript Code:
package {                 import flash.display.MovieClip                 import flash.text.TextField                 import flash.events.Event                 import flash.events.MouseEvent                                 public class DocumentMain extends MovieClip                 {                                 public const GRAVITY:Number = 2;                                 public const BOUNCE_FACTOR:Number = 0.8;                                 public const HIT_FORCE:Number = 20;                                                                 public var _bounces:TextField;                                 public var _highscore:TextField;                                 public var _ball:Ball;                                                                 private var _vx:Number;                                 private var _vy:Number;                                                                 public function DocumentMain():void                                 {                                                 _vx = 10;                                                 _vy = 0;                                                                                                 _ball.buttonMode = true;                                                                                                 addEventListener(Event.ENTER_FRAME, enterFrameHandler);                                                 addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);                                 }                                                                 private function enterFrameHandler(e:Event):void                                 {                                                 // gravitate the ball                                                 _vy += GRAVITY;                                                                                                 // move the ball                                                 _ball.x += _vx;                                                 _ball.y += _vy;                                                                                                 // check stage boundaries for collision                                                 checkBoundaryCollision();                                 }                                                                 private function mouseDownHandler(e:MouseEvent):void                                 {                                                 // hit the ball if it has been clicked                                                 if (e.target == _ball)                                                 {                                                                 hit(e.target.mouseX, e.target.mouseY);                                                 }                                 }                                                                 private function checkBoundaryCollision():void                                 {                                                 var left:Number;                                                 var right:Number;                                                 var bottom:Number;                                                 var top:Number;                                                                                                 left = _ball.x - (_ball.width / 2);                                                 right = _ball.x + (_ball.width / 2);                                                 bottom = _ball.y + (_ball.height / 2);                                                 top = _ball.y - (_ball.height / 2);                                                                                                 if (left < 0 && _vx < 0)                                                 {                                                                 _ball.x = _ball.width / 2;                                                                 _vx *= -1;                                                 }                                                 else if (right > stage.stageWidth && _vx > 0)                                                 {                                                                 _ball.x = stage.stageWidth - (_ball.width / 2);                                                                 _vx *= -1;                                                 }                                                 if (top < 0 && _vy < 0)                                                 {                                                                 _ball.y = _ball.height / 2;                                                                 _vy *= -1;                                                 }                                                 else if (bottom > stage.stageHeight && _vy > 0)                                                 {                                                                 _ball.y = stage.stageHeight - (_ball.height / 2);                                                                 _vy *= -BOUNCE_FACTOR;                                                                 _vx *= BOUNCE_FACTOR;                                                                                                                                 if (Number(_bounces.text) > Number(_highscore.text))                                                                 {                                                                                 _highscore.text = _bounces.text;                                                                 }                                                                 _bounces.text = "0";                                                 }                                 }                                                                 private function hit(hitX:Number, hitY:Number):void                                 {                                                 // increment bounces                                                 _bounces.text = String(Number(_bounces.text) + 1);                                                                                                 // adjust the vertical velocity of the ball                                                 if (_vy > 0)                                                 {                                                                 _vy *= -BOUNCE_FACTOR / 2;                                                 }                                                 _vy -= HIT_FORCE;                                                                                                 // adjust the horizontal velocity of the ball                                                 if (_vx * hitX > 0)                                                 {                                                                 _vx *= -BOUNCE_FACTOR;                                                 }                                                                                                 _vx -= (hitX / _ball.width) * HIT_FORCE;                                 }                 } }

So if I were to add _rounds as a dynamic text field on the stage and put in
ActionScript Code:
public var _rounds:TextField;
near the beginning where i establish my other variables that would set me up to be able to use it later in the code right?

then could I alter my last section of the private function checkBoundaryCollision():void section of the code to be:

ActionScript Code:
if (bottom > stage.stageHeight && _vy > 0)                                                 {                                                                 _ball.y = stage.stageHeight - (_ball.height / 2);                                                                 _vy *= -BOUNCE_FACTOR;                                                                 _vx *= BOUNCE_FACTOR;                                                                                                                                 if (Number(_bounces.text) > Number(_highscore.text))                                                                 {                                                                                 _highscore.text = _bounces.text;                                                                 }                                                                 _bounces.text = "0";                                                                 _rounds.text += 1                                                 }
so that when the ball hits the ground the rounds go up by one.
Am I right so far?

Now I'm confused as to how i should integrate the code that you gave me. Because the next thing i need to do is make it so that when the _rounds variable textbox shows greater than three (so at the end of the third round) the game terminates.

thanks so much in advance!
juuri is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 04:36 AM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.