Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 07-03-2012, 12:40 AM   #1
Registered User
Join Date: Jun 2012
Posts: 8
Default [AS3] game development help

Hi I need help with my game project. This game is very simple but since I am new to OOP I am not able to proceed further.The game is of a character running left or right avoiding falling objects and collecting some objects in the way.
I am using random number to add these objects to the stage. The problem is I want these objects to appear only once.
Also I need to check for collision with the hero which has a different custom class Hero attached to it.

Please help.

This is the custom class file for new objects to be collected:
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.ui.Keyboard;
import flash.ui.Mouse;
import flash.text.TextField;
import flash.events.TimerEvent;
import flash.utils.Timer;
import BaseObject;
import Hero;

public class Collectibles extends MovieClip{

private var collectiblesTimer:Timer = new Timer(5000);
private var object1:Object1 = new Object1;
public var gameFinished:GameFinishedScreen = new GameFinishedScreen;
public var newObject:MovieClip;
public function Collectibles() {
// constructor code
collectiblesTimer.addEventListener(TimerEvent.TIME R,addRandomObjects);

//initialise the game finished screen
//gameFinished.nextLevel.buttonMode = true;
//gameFinished.nextLevel.addEventListenerMouseEvent. CLICK,onGameFinished);

public function addRandomObjects(evt:TimerEvent): void {
//create an instance of a random object
var randomNumber:Number = Math.random();
var objectCount:int = 0;

// 0 .... 1
if (randomNumber >= 0 && randomNumber <= 0.2) {
newObject = new Object1();
objectCount += 1;
newObject.amountPointsWorth = 20;
if (randomNumber >= 0.2 && randomNumber <= 0.4) {
newObject = new Object2();
objectCount += 1;
newObject.amountPointsWorth = 30;
if (randomNumber >= 0.4 && randomNumber <= 0.6) {
newObject = new Object3();
objectCount += 1;
newObject.amountPointsWorth = 100;
if (randomNumber >= 0.6 && randomNumber <= 0.8) {
newObject = new Object4();
objectCount += 1;
newObject.amountPointsWorth = 100;
if (randomNumber >= 0.8 && randomNumber <= 1) {
newObject = new Object5();
objectCount += 1;
newObject.amountPointsWorth = 50;
if(objectCount == 5) {
trace("objectCount" + objectCount);
// limit the objects
newObject.x = Math.random() * stage.stageWidth;
newObject.y = Math.random() * (stage.stageHeight - 150);

// limit the boundaries for the new Object
if(newObject.x > stage.stageWidth){
newObject.x = stage.stageWidth - newObject.width;
if(newObject.y <= 200){
newObject.y = 200 + newObject.width/2;
if(newObject.y >= 300){
newObject.y = 300 - newObject.width/2;

public function testCollisionObjects() {
trace("this will work");
// check for collision of new object with the hero
trace("new object hits hero");


ashwini_desh is offline   Reply With Quote
Old 07-03-2012, 04:47 AM   #2
Senior Member
[afz]snickelfitz's Avatar
Join Date: Dec 2011
Location: Tucson, AZ
Posts: 2,400

Some basic advice:
Do not nest functions inside other functions.
Most of the time, instance methods and properties should be private.
(certainly, they should start out as private, ond only changed to public if absolutely necessary)
Declare properties within the class block; define them in the constructor or within downstream methods.
Use the forum [as] tags to post formatted actionscript code.

Here's a compact version of your code, minus the stuff I couldn't validate and test.
Only 1 instance of each ObjectX class will be added to the displaylist.
ActionScript Code:
package {     import flash.display.*;     import flash.events.*;     import flash.ui.*;     import flash.text.*;     import flash.utils.*;     public class Collectibles extends MovieClip     {         private var t:Timer;         private var delay:int;         private var repeat:int;         private var newObject:MovieClip;         private var objects:Array;                 public function Collectibles()         {             objects = [{obj:Object1, val:20},                        {obj:Object2, val:30},                        {obj:Object3, val:100},                        {obj:Object4, val:100},                        {obj:Object5, val:50}];                         delay = 5000;             repeat = objects.length;                         t = new Timer(delay, repeat);             t.addEventListener(TimerEvent.TIMER, addRandomObjects, false, 0, true);             t.start();         }         private function addRandomObjects(e:TimerEvent):void         {             var len:int = objects.length;             var rand:int = Math.floor(Math.random() * len);                         trace(rand, objects.length);                         newObject = new objects[rand].obj();             newObject.amountPointsWorth = objects[rand].val;             newObject.x = Math.random() * (stage.stageWidth- newObject.width);             newObject.y = Math.random() * (stage.stageHeight - newObject.height);             addChild(newObject);                         objects.splice(rand, 1);         }     } }
[afz]snickelfitz is offline   Reply With Quote
Old 07-03-2012, 01:09 PM   #3
Senior Member
henke37's Avatar
Join Date: Mar 2009
Location: Sweden
Posts: 10,861
Send a message via Skype™ to henke37

You can't define and declare something on different lines. You mean that the properties are initialized in the constructor.
Signature: I wrote a pair of articles about the timeline.
henke37 is offline   Reply With Quote
Old 07-03-2012, 11:03 PM   #4
Registered User
Join Date: Jun 2012
Posts: 8
Default Thanks!

Thanks [afz]snickelfitz and henke37 for your help!. I will certainly follow your advice. Actionscripting is something I really love to do. Only thing I need some more practice with OOP since I dont have any programming background.

Thanks for your help!!
ashwini_desh is offline   Reply With Quote
Old 07-04-2012, 10:23 PM   #5
Registered User
Join Date: Jun 2012
Posts: 8

Hi again,
Need help again. I am trying to write a hit test function for the same game

public function collisionWithObjects() {
for(var m:int = 0;m < collectibles.numChildren;m++) {
trace("This is working");
var newObjectsCollisionobjects = ollectibles.getChildAt(m) as objects;
if(newObjectsCollision.hitTestObject(hero) == true) {
trace("Object collides with hero");

Get two errors 1046:type was not found or was not compile time constant: objects
error 1120: access of undefined property objects

any hints?
ashwini_desh is offline   Reply With Quote
Old 07-06-2012, 10:47 AM   #6
Registered User
Join Date: Nov 2010
Posts: 8

when you wrote
ActionScript Code:
var newObjectsCollision:objects = collectibles.getChildAt(m) as objects;
did you wanted to ensure that the instance returned by "collectibles.getChildAt(m)" was in the "objects" array? Or is "objects" a custom class defined by you?
Maybe you're getting these errors because "objects" is not an object data type. In AS3, you can only use the "as" statement with Data Types or Interfaces, and you can't treat an Array object as a Data Type.

If you want to make a collision test between all MovieClip objects in the "collectibles" object and your "hero" object, you can write:
ActionScript Code:
public function collisionWithObjects():Boolean {      for(var m:int = 0;m < collectibles.numChildren;m++) {           //trace("This is working");           var newObjectsCollision:MovieClip = collectibles.getChildAt(m) as MovieClip;           if(newObjectsCollision != null){  // Or you can only write "if(newObjectsCollision)..."                if(newObjectsCollision.hitTestObject(hero)) {                     trace("an Object collides with hero");                     return true;                }           }      }      return false; } // Elsewhere in your code... if(collisionWithObjects()) {      // Do something... }
I've added another if condition to test if the newObjectsCollision isn't null. This can happen if you have an object inside the "collectibles" that isn't compatible with the MovieClip type.
I'd recommend you to create a class to represent the falling objects in your game. That way you could write something like
ActionScript Code:
var newObjectsCollision:FallingObject = collectibles.getChildAt(m) as FallingObject;
in order to avoid confusions between a falling object and a simple MovieClip that isn't a falling object in your program.

I hope this helps. Good luck to your project
titz97 is offline   Reply With Quote
Old 07-11-2012, 02:09 AM   #7
Registered User
Join Date: Jun 2012
Posts: 8

Thanks! titz97 yes I got it working.....
ashwini_desh is offline   Reply With Quote
Old 07-19-2012, 10:20 PM   #8
Registered User
Join Date: Jul 2012
Posts: 1

You can't define and declare something on different lines

Last edited by peterr77; 07-19-2012 at 10:21 PM. Reason: http://www.centplay.com/affiliate/id_139/
peterr77 is offline   Reply With Quote

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 02:38 PM.

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.