Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 08-06-2012, 08:20 AM   #1
sziada
Registered User
 
Join Date: Aug 2012
Location: Australia
Posts: 4
Question Is there a beter way to make Pickups

I can up with this code for collectables and I was thinking is there a better way of doing this because I wanted to added multiple around the map but my problem is that I can't have multiple without copying the code again and again.

ActionScript Code:
var newPickup:Pickup = new Pickup(); newPickup.x = 440; newPickup.y = 340; addChild(newPickup); newPickup.addEventListener(Event.ENTER_FRAME, ftn_Pickup); function ftn_Pickup(ev:Event):void{     if (box_mc.hitTestObject(newPickup)){         var_Score++;//add one to the score         lbl_Score.text = String ("Score:") + (var_Score);         newPickup.visible = false;         newPickup.x = 0;         newPickup.y = 0;         //removeChild(newPickup);     } }

Please any Help is wanted plus I am newbie to this so just trying to get a decent foothold of this.
sziada is offline   Reply With Quote
Old 08-06-2012, 08:41 AM   #2
santanoa
Dual Screen Empowered
 
santanoa's Avatar
 
Join Date: Jul 2010
Location: Israel
Posts: 258
Default

hey sziada!

Welcome to our lovely forums.

What you want to do is have your Pickup class handle all the Pickup instances (fancy word for 'single object of a class that has been created'). Not sure if you've created the Pickup class yourself, or had Flash automatically create it for you when you linked it to the Pickup MC. If it's the latter, to be able to edit the Pickup class, right-click Pickup MC in library and click 'Properties'. You should see a window. In the middle will be two pencil icons, next to two text boxes, one of which should say 'Pickup'. Click that one. You can now edit the class. Just make sure to save it wherever your .fla is.

Here's a sample of how your Pickup class should look like

ActionScript Code:
package {     import flash.display.MovieClip;     public class Pickup extends MovieClip {          public static var allPickupList = []//static means it is independent of any one instance. you can look it up to understand better           public function Pickup() {             allPickupList.push(this); //this refers to the specific Pickup object just created. You want to have all Pickups in the allPickupList         }         public static function checkPickupCollisions(mc:MovieClip) {             for (var i = 0; i < allPickupList.length; i++) {                 var tempPickup = allPickupList[i];                 if (mc.hitTestObject(tempPickup)) //do whatever you want :)             }         }     } }
Now in your main class you can loop through all the Pickups and see if box_mc has hit them. (If you don't have a main class, you should. It makes your life much easier, especially if you're creating a game. The role of a main
class is to control all other classes/objects on-screen and centralize your code). But if you don't have one now, you can just put this on the timeline

ActionScript Code:
addEventListener(Event.ENTER_FRAME,enterFrame); function enterFrame(e) {     Pickup.checkPickupCollision(box_mc); }
santanoa is offline   Reply With Quote
Old 08-06-2012, 08:57 AM   #3
sziada
Registered User
 
Join Date: Aug 2012
Location: Australia
Posts: 4
Default

Quote:
Originally Posted by santanoa View Post
hey sziada!

Welcome to our lovely forums.

What you want to do is have your Pickup class handle all the Pickup instances (fancy word for 'single object of a class that has been created'). Not sure if you've created the Pickup class yourself, or had Flash automatically create it for you when you linked it to the Pickup MC. If it's the latter, to be able to edit the Pickup class, right-click Pickup MC in library and click 'Properties'. You should see a window. In the middle will be two pencil icons, next to two text boxes, one of which should say 'Pickup'. Click that one. You can now edit the class. Just make sure to save it wherever your .fla is.

Here's a sample of how your Pickup class should look like

ActionScript Code:
package {     import flash.display.MovieClip;     public class Pickup extends MovieClip {          public static var allPickupList = []//static means it is independent of any one instance. you can look it up to understand better           public function Pickup() {             allPickupList.push(this); //this refers to the specific Pickup object just created. You want to have all Pickups in the allPickupList         }         public static function checkPickupCollisions(mc:MovieClip) {             for (var i = 0; i < allPickupList.length; i++) {                 var tempPickup = allPickupList[i];                 if (mc.hitTestObject(tempPickup)) //do whatever you want :)             }         }     } }
Now in your main class you can loop through all the Pickups and see if box_mc has hit them. (If you don't have a main class, you should. It makes your life much easier, especially if you're creating a game. The role of a main
class is to control all other classes/objects on-screen and centralize your code). But if you don't have one now, you can just put this on the timeline

ActionScript Code:
addEventListener(Event.ENTER_FRAME,enterFrame); function enterFrame(e) {     Pickup.checkPickupCollision(box_mc); }
ok but the thing is I am not using a n external class would that be a problem
sziada is offline   Reply With Quote
Old 08-06-2012, 09:05 AM   #4
sziada
Registered User
 
Join Date: Aug 2012
Location: Australia
Posts: 4
Question

Here is my original code if that can help you explain it any better

ActionScript Code:
//////////////////////////////////////// //Miscelanious /////////////////////////////////////// stop(); Mouse.hide();//hides the mouse cursor mouseEnabled = false; lbl_Score.mouseEnabled = false; txtTime.mouseEnabled = false; //////////////////////////////////// //Movement /////////////////////////////////// var gravity:Number = 10; //Gravity how fast will the Object fall var jumpPower:Number = 0;//Controls how high player jumps but set to zero since objects starts at the ground point var isJumping:Boolean = false; var ground:Number = 410 - box_mc.height; stage.addEventListener(MouseEvent.MOUSE_DOWN, doJump); stage.addEventListener(Event.ENTER_FRAME, update); function doJump(ev:MouseEvent):void { if(!isJumping) { jumpPower = 34; isJumping = true;//Player has started jumping     } } function update(ev:Event):void { if(isJumping) { box_mc.y -= jumpPower; jumpPower -= 2;//slows down the object until reaches the ground } if(box_mc.y + gravity < ground) box_mc.y += gravity; else { box_mc.y = ground; isJumping = false; // this stop the object from falling past the ground     }     box_mc.x = stage.mouseX - box_mc.width * 0;//horizonal Movement plus collison } ////////////////////////////////// //Score Setup ///////////////////////////////// var var_Score:Number = 0; lbl_Score.text = String ("Score:") + (var_Score); //////////////////////////////// //Pickups /////////////////////////////// var newPickup:Pickup = new Pickup(); newPickup.x = 440; newPickup.y = 340; addChild(newPickup); newPickup.addEventListener(Event.ENTER_FRAME, ftn_Pickup); function ftn_Pickup(ev:Event):void{     if (box_mc.hitTestObject(newPickup)){         var_Score++;//add one to the score         lbl_Score.text = String ("Score:") + (var_Score);         newPickup.visible = false;         newPickup.x = 0;         newPickup.y = 0;         //removeChild(newPickup);     } } /////////////////////////////////// //Timer ////////////////////////////////// txtTime.text = "Time: 0"; var myTimer:Timer = new Timer(1000, 60); //creates a new timer Variable, 1000 ms = 1 sec myTimer.addEventListener(TimerEvent.TIMER, ftnTimer); //listen out for te timer, and t var varTime:Number = 0; //set variables varTime to 8 myTimer.start(); //start timer     function ftnTimer(e:TimerEvent):void //new function ftnTimer {     varTime++; //take one to varTime     txtTime.text = "Time: " + varTime; //updates the timer text box } /////////////////////////////// //Enemies ////////////////////////////// ///////////////////////////// //lives //////////////////////////// var lives:Number = 3; txt_lives.text = "Lives:" + (lives); ///////////////////////////// //Door //////////////////////////// /////////////////////////// //Collisions ////////////////////////// /////////////////////////// //End Game //////////////////////////
sziada is offline   Reply With Quote
Old 08-06-2012, 09:58 AM   #5
santanoa
Dual Screen Empowered
 
santanoa's Avatar
 
Join Date: Jul 2010
Location: Israel
Posts: 258
Default

That's fine. Like I said, it was only a suggestion. The code should work perfectly fine in the timeline. However, the Pickup code that I said should be in the Pickup class must be there. There are other ways of accomplishing what my code does without classes, but this is by far the easiest. Anyway, you're already using an external class with Pickup (whether you know it or not) when you write
ActionScript Code:
new Pickup()
I already explained in my first post how to convert the Flash-created Pickup class into an editable version.


Hope there's no confusion. The code looks pretty good, but if you want to learn as3 well, you should start using classes. Here's a good tutorial:
http://www.kirupa.com/developer/as3/classes_as3_pg1.htm

Good luck
santanoa is offline   Reply With Quote
Old 08-06-2012, 11:39 AM   #6
arkitx
Senior Member
 
Join Date: Jan 2011
Posts: 703
Default

ActionScript Code:
//////////////////////////////////////// //Miscelanious /////////////////////////////////////// stop(); Mouse.hide();//hides the mouse cursor mouseEnabled = false; lbl_Score.mouseEnabled = false; txtTime.mouseEnabled = false; //////////////////////////////////// //Movement /////////////////////////////////// var gravity:Number = 10;//Gravity how fast will the Object fall var jumpPower:Number = 0;//Controls how high player jumps but set to zero since objects starts at the ground point var isJumping:Boolean = false; var ground:Number = 410 - box_mc.height; stage.addEventListener(MouseEvent.MOUSE_DOWN, doJump); stage.addEventListener(Event.ENTER_FRAME, update); function doJump(ev:MouseEvent):void {     if (!isJumping) {         jumpPower = 34;         isJumping = true;//Player has started jumping     } } function update(ev:Event):void {     if (isJumping) {         box_mc.y -= jumpPower;         jumpPower -= 2;//slows down the object until reaches the ground     }     if (box_mc.y + gravity < ground) {         box_mc.y += gravity;     } else {         box_mc.y = ground;         isJumping = false;// this stop the object from falling past the ground     }     box_mc.x = stage.mouseX - box_mc.width * 0;//horizonal Movement plus collison } ////////////////////////////////// //Score Setup ///////////////////////////////// var var_Score:Number = 0; lbl_Score.text = String ("Score:") + (var_Score); //////////////////////////////// //Pickups /////////////////////////////// create(); var pickup:Pickup; function create() {     pickup= new Pickup();     pickup.x = Math.round(Math.random()*440);     pickup.yMath.round(Math.random()*(340-220+1))+220;     addChild(pickup); } pickup.addEventListener(Event.ENTER_FRAME, ftn_Pickup); function ftn_Pickup(ev:Event):void {     if (box_mc.hitTestObject(pickup)) {         var_Score++;//add one to the score         lbl_Score.text = String ("Score:") + (var_Score);         //newPickup.visible = false;         //pickup.x = 0;         //pickup.y = 0;         removeChild(pickup);         create();     } } /////////////////////////////////// //Timer ////////////////////////////////// txtTime.text = "Time: 0"; var myTimer:Timer = new Timer(1000, 60);//creates a new timer Variable, 1000 ms = 1 sec myTimer.addEventListener(TimerEvent.TIMER, ftnTimer);//listen out for te timer, and t var varTime:Number = 0;//set variables varTime to 8 myTimer.start();//start timer function ftnTimer(e:TimerEvent):void {//new function ftnTimer     varTime++;//take one to varTime     txtTime.text = "Time: " + varTime;//updates the timer text box } /////////////////////////////// //Enemies ////////////////////////////// ///////////////////////////// //lives //////////////////////////// var lives:Number = 3; txt_lives.text = "Lives:" + (lives); ///////////////////////////// //Door //////////////////////////// /////////////////////////// //Collisions ////////////////////////// /////////////////////////// //End Game //////////////////////////

If I misunderstood, then please provide your FLA.


__________________
Regards

arkitx
arkitx 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 08:15 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.