Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 05-27-2008, 05:45 AM   #1
Platypus2008
Member
 
Join Date: Apr 2008
Posts: 50
Default [AS3] Shooting

I'm have a mc exported for actionscript as "laser" I want to create a new instance of it everytime someone presses the shooting key and then have it move horozontaly. Anyone know how to do that?
Platypus2008 is offline   Reply With Quote
Old 05-27-2008, 05:57 AM   #2
amarghosh
unRegistered User
 
amarghosh's Avatar
 
Join Date: Feb 2008
Location: Bangalore
Posts: 817
Default

ActionScript Code:
shootingKeyInstanceName.addEventListener(MouseEvent.CLICK, shootIt); public function shootIt(e:MouseEvent):void {     var t:Laser = new Laser();//export it as Laser (UseInitialCaps), thats the practice;     addChild(t);     t.addEventListener(Event.ENTER_FRAME, moveHoriz); } public function moveHoriz(e:Event):void {     var t:MovieClip = MovieClip(e.currentTarget);     t.x++;     if(t.x > maxHorizPosition)//if u wanna remove it     {         t.removeEventListener(Event.ENTER_FRAME, moveHoriz);         removeChild(t);     } }
__________________
Blog
amarghosh is offline   Reply With Quote
Old 05-27-2008, 01:11 PM   #3
Platypus2008
Member
 
Join Date: Apr 2008
Posts: 50
Default

It sais "The public attribute can only be used inside a package"
Platypus2008 is offline   Reply With Quote
Old 05-28-2008, 04:11 AM   #4
amarghosh
unRegistered User
 
amarghosh's Avatar
 
Join Date: Feb 2008
Location: Bangalore
Posts: 817
Default

declare the methods as class's instance methods instead of declaring them within other methods.
move them out of all the method definitions
__________________
Blog
amarghosh is offline   Reply With Quote
Old 05-28-2008, 06:46 AM   #5
Platypus2008
Member
 
Join Date: Apr 2008
Posts: 50
Default

Quote:
Originally Posted by amarghosh View Post
declare the methods as class's instance methods instead of declaring them within other methods.
move them out of all the method definitions
I just took the word public out. But is there some benefit in doing it the way you said?
Platypus2008 is offline   Reply With Quote
Old 05-28-2008, 06:48 AM   #6
amarghosh
unRegistered User
 
amarghosh's Avatar
 
Join Date: Feb 2008
Location: Bangalore
Posts: 817
Default

are u coding on timeline?
__________________
Blog
amarghosh is offline   Reply With Quote
Old 05-28-2008, 07:18 AM   #7
Platypus2008
Member
 
Join Date: Apr 2008
Posts: 50
Default

Quote:
Originally Posted by amarghosh View Post
are u coding on timeline?
Yeah.
Platypus2008 is offline   Reply With Quote
Old 05-28-2008, 08:07 AM   #8
amarghosh
unRegistered User
 
amarghosh's Avatar
 
Join Date: Feb 2008
Location: Bangalore
Posts: 817
Default

in that case am not sure about the differences.
if your code is in an AS file, declaring methods as class's instance method is better practice than declaring them inside a function.
it is particularly important when u add these methods as listeners to some external objects.
u can't reference inner methods outside the containing function.
__________________
Blog
amarghosh is offline   Reply With Quote
Old 05-28-2008, 12:26 PM   #9
Platypus2008
Member
 
Join Date: Apr 2008
Posts: 50
Default

I have the problem with the hitTest. The var "u" does not exist withen the moveHoriz function so the hitTest creates an error. However when I move the line "var u:UFO = new UFO();" into the main code. The hitTest works but it will not let there be more than one instance of UFO on stage at one time. Another thing I tried was add the same line of code into the moveHoriz funtion and that does let there be more than one instance on stage at one time but the hitTest does not seem to work even though there is no reported error. Any ideas?

ActionScript Code:
function shootit(){     milk.width -= 30;     var t:Laser = new Laser(); addChild(t);     t.x = cow.x, t.y = cow.y;     t.addEventListener(Event.ENTER_FRAME, moveHoriz); } function addufo() {      var u:UFO = new UFO(); addChild(u);         u.y = 200;     u.addEventListener(Event.ENTER_FRAME, umh); } function moveHoriz(e:Event):void {     var t:MovieClip = MovieClip(e.currentTarget);     t.x -= 8;     t.y += 1;     if (t.hitTestObject(u)){t.x = 0, pwns+=1, ufomsg.text = "UFO pwns " + pwns};     if (t.x < 0)      {         t.removeEventListener(Event.ENTER_FRAME, moveHoriz);         removeChild(t);     } } function umh(e:Event):void {   var u:MovieClip = MovieClip(e.currentTarget);     u.x +=4;     if (u.x > 600) {         removeChild(u);         u.removeEventListener(Event.ENTER_FRAME, umh);             } }
Platypus2008 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

Similar Threads
Thread Thread Starter Forum Replies Last Post
simple shooting game not shooting.... Rush152 Gaming and Game Development 1 02-08-2008 11:29 AM
shooting guns evride Gaming and Game Development 5 10-25-2007 07:38 PM
Shooting... JohnnyTightLips ActionScript 2.0 1 03-27-2007 02:35 AM
Shooting in 8 Directions. Rikimaru ActionScript 1.0 (and below) 0 01-19-2005 11:02 PM
Shooting to centre pzijko ActionScript 2.0 2 02-25-2004 02:27 AM


All times are GMT. The time now is 02:44 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.