Sprite trying to remove itself from the stage is throwing errors.

I'm trying to have a bullet Sprite remove itself from the stage once it is off the stage, but Flash keeps throwing #1009 errors at me. I've looked over the code several times and don't understand what's wrong.

Bullet code:

Code: package { import flash.display.Sprite; import flash.display.MovieClip; import*; public class Bullet extends Sprite { private var bullet_direction; private var bullet_speed; public function Bullet(xLoc:Number, yLoc:Number, angle:Number, speed:Number) { x = xLoc; y = yLoc; bullet_direction = angle; bullet_speed = speed; addEventListener(Event.ENTER_FRAME, BulletMovement,false,0,true); } public function BulletMovement(e:Event) { y -= bullet_speed * Math.sin(bullet_direction); x += bullet_speed * Math.cos(bullet_direction); if ( x > 500 || x < -20 || y > 660 || y < -20 ) { DestroyBullet(); } } public function DestroyBullet():void { removeEventListener(Event.ENTER_FRAME, BulletMovement); (stage as MovieClip).removeChild(this); } } } Error message:

Code: TypeError: Error #1009: Cannot access a property or method of a null object reference. at Bullet/DestroyBullet() at Bullet/BulletMovement()

Posted On: Thursday 25th of October 2012 10:28:38 PM Total Views:  254
View Complete with Replies

Related Messages:

HELP: sprite.addChild(NumericStepper) resizes sprite to 100x100   (138 Views)
If anyone can help me with this, I would be extremely grateful. It's driving me insane. If I run this code: var mySprite:Sprite = new Sprite(); trace("mySprite 1:", mySprite.width, mySprite.height); numericStepper = new NumericStepper(); trace("numericStepper 2:", numericStepper.width, numericStepper.height); mySprite.addChild(numericStepper); trace("mySprite 3:", mySprite.width, mySprite.height); I expect this output: mySprite 1: 0 0 numericStepper 2: 80 22 mySprite 3: 80 22 But I get this output: mySprite 1: 0 0 numericStepper 2: 80 22 mySprite 3: 100 100 If instead I do this: mySprite = new Sprite(); trace("mySprite 1:", mySprite.width, mySprite.height); numericStepper = new NumericStepper(); trace("numericStepper 2:", numericStepper.width, numericStepper.height); mySprite.addChild(numericStepper); trace("mySprite 3:", mySprite.width, mySprite.height); mySprite.width = 80; mySprite.height = 22; Then the numericStepper is drawn as if it is squished to 20% its normal height. If I pre-set mySprite width and height before adding the numericstepper, the trace output is 0,0 after adding it and it is not displayed at all. Is there some way I can make the sprite only adjust to correctly fit the numericStepper OR resize it afterwards without distorting the numericStepper component As I was typing this, I came across this post: Can anyone help me modify that solution for a numeric stepper
Flash Attach and remove sprites using TweenLite   (145 Views)
I'm creating visual effects using TweenLite, which currently only manages the tweens for me, can I also attach a sprite at a specific time (using "delay") Basically I want to use TweenLite to attach/remove sprites apart from just tweening existing objects.
adding a border around a sprite   (184 Views)
I want to add a border around my mask. I don't know if I can add the border directly to the mask or if I need to create something new and position it. I imagine if the border was part of the mask it wouldnt show up Anyway I added lineStyle to my roomMask and I get this error that doesn't make any sense to me: 1061: Call to a possibly undefined method lineStyle through a reference with static type flash.display:Sprite. Is this saying you can't have a lineStyle on a Sprite I am not sure what I am doing wrong here. ActionScript Code: //create a mask to cover the room container //to hide the floor outside edges var roomMask:Sprite = new Sprite(); roomMask.lineStyle(6, 0x0000FF, 0.5);;,0,662,411,15,15);; addChild(roomMask); roomMask.x = 28; roomMask.y = 110; roomContainer.mask = roomMask;
How to access a sprite on a "loaded" swf from parent stage   (127 Views)
I am creating a Flash training app that includes a glossary. You click a button and the glossary opens. The glossary loads thus: PHP Code: varrequest:URLRequest=newURLRequest("glossary.swf"); varloader:Loader=newLoader() loader.load(request); swfCont.addChild(loader); The glossary has a scrollbar, and that scrollbar has this listener (among other things): PHP Code: scrollbar.addEventListener(MouseEvent.MOUSE_UP,stopScroll); Once the glossary is loaded into my app, if I do a MOUSE_DOWN on the scrollbar and then drag the mouse away from the glossary, onto the parent stage, then when I do a MOUSE_UP on the parent stage, I need to dispatch a new MouseEvent.MOUSE_UP event to the scrollbar in the glossary swf. How do I access the scrollbar of glossary.swf in my dispatchEvent In other words, in the following code in the parent: PHP Code: stage.addEventListener(MouseEvent.MOUSE_UP,glossarySBMouseUp); functionglossarySBMouseUp(me:MouseEvent):void{ xxx.dispatchEvent(newMouseEvent(MouseEvent.MOUSE_UP)); } ...what's xxx
Change the rotational/scale point on a object/sprite?   (141 Views)
I've wondered this for a bit and don't know where to find it. When you load a object through flash, you can set the rotational/scale point, but when you load it as a loader object or sprite, how do you change this point In this instance, I want to change the height of a sprite that is filled with a rectangle so that it shrinks down, not up. Right now: var colored_bar:Sprite = new Sprite(); with ( { beginFill(0x9e1819, 1); drawRect(0, 0, 67, 80); endFill(); } colored_bar.x = 201; colored_bar.y = 390; stage.addChild(colored_bar); then: colored_bar.height=colored_bar.height/2 I want it to go: ----------- |ooooooo| |ooooooo| |ooooooo| |ooooooo| ___ ---------- |ooooooo| ___ |oooooooo| |ooooooo| ___ |oooooooo| |ooooooo| ___ |oooooooo| ---------- ___ ----------- Not the default: ---------- ___ ----------- |ooooooo| ___ |oooooooo| |ooooooo| ___ |oooooooo| |ooooooo| ___ |oooooooo| |ooooooo| ___ ----------- |ooooooo| |ooooooo| |ooooooo| ---------- The way I am doing it currently is moving the y position everytime I take away soem of the height, so it looks like it's moving down, but I'd think moving the orientation point at the beginning would be faster
moving a sprite in both directions of x-axis   (115 Views)
, am trying to make a game whre i want the sprite to move from positive x-axis to negetive x-axis for 30 seconds and after dat it has to down along y-axis one step.anybody please help me
changing width of sprite also seems to change it's height...   (236 Views)
Here is some code I have. Code: _buttonUnderline = new Sprite(); with( { lineStyle(4, 0xACACAC); moveTo( -2, 0); lineTo(2, 0); } _buttonUnderline.x = + 104 / 2; _buttonUnderline.y = + + 10; addChild(_buttonUnderline); new Tween(_buttonUnderline, "width", Elastic.easeInOut, _buttonUnderline.width,, 1, true); When the Tween takes place though it not only changes the width, it changes the height as if it were scaling it up in both directions. Why does this behavior take place And how can I get it so that only the width changes I have also tried using scaleX as the property but it does the same thing.
common interface for classes that extend sprite   (160 Views)
Hei! Im trying to implement an interface for custom classes that extend Sprite (DisplayObject), but i can't use DO methods on those instances afterwards, when called via the interface.. illustration: Code: interface: public interface IListingItem extends IEventDispatcher { function setData( someData ):void; } // usage.. var instance:IListingItem = new MyDisplayObject( ); // MyDisplayObject extends Sprite, implements IListingItem instance.addEventListener( Event.COMPLETE, onItemLoaded ); // okay, since interface extends IEventDispatcher instance.setData( "bla" ); // still okay, since interface defines this method instance.x = 10; // 1119: Access of possibly undefined property x through a reference with static type classes.interfaces:IListingItem. this.addChild( instance ); // i guess this would also throw error So maybe i do something wrong Should be interface only used for calling some simple methods that don't work with display list Initially i also got an error calling that addEventListener on the content instance, that disappeared after i told the interface to extend IEventDispatcher. Is there similiar interface that defines DO properties like ISprite or something Or should this technique not be used at all Any ideas welcome!
How to get focus on a movieclip or sprite   (140 Views)
This starting to get very annoying so I had to seek some help. How can I cause a sprite or movieclip to get focus on a Mouse Event I realized that this only happens when I put a textfield inside the movieclip/sprite. If I click in an area outside the textfield (but still within the movieclip/sprite) it will cause the mouse event BUT it will not get focus - My objective is to let the button once pressed, to cause the currently focused object to lose focus. That focused object is an input textfield. Does it make sense what Im trying to achieve
Tabbed nav - trouble removing "tab" sprites   (128 Views)
I'm building a little widget that has 3-tab navigation. I've written the following classes (small subset, anyway) to control everything: - the document class, responds to navigation class and loads tab content classes as - extends Sprite; receives parameters for tab colors/labels from (unrelated XML call for configuration) and generates 3 tabs using a (extends Sprite) class for the button graphics; dispatches CLICK event back to for content load / / - all extend Sprite; each loads various XML feeds and graphics depending on function to render the "content" of each tab; instantiated from and, ideally, removed by when gets calls from This is the event I'm dispatching from back to to handle tab content loading: Code: public function onMouseClick(e:Event):void { trace(":: onClick() ::"); currentTab =; trace(":: onClick() : currentTab : " + currentTab + " ::"); dispatchEvent(new Event(NAV_CLICK, true)); } receives this event and processes it with this rather pair of functions: Code: private function onNavClick(e:Event):void { trace(":: onNavClick() ::"); loadTab(_navigation.currentTab); } // load tab private function loadTab(tab_type:String):void { trace(":: loadTab() ::"); switch(tab_type) { case "nav_tab_0" : _gameDayTab = new GameDayTab(_configData.game_day_tab_xml, _cssStyles); addChild(_gameDayTab); break; case "nav_tab_1" : _teamInfoTab = new TeamInfoTab(_configData.team_info_tab_xml, _cssStyles); addChild(_teamInfoTab); break; case "nav_tab_2" : _newsTab = new NewsTab(_configData.news_tab_xml, _cssStyles); addChild(_newsTab); break; } } I need to be a little more elaborate with this code, I know, but i guess what I'm trying to figure out is how to REMOVE a tab content class from the stage. I tried removeChild(), which didn't work -- I understand that's only removing the reference to that object now, and I really also need to clean up what each tab content instance loads (BulkLoader, other sprites, textfields, etc.). Not ideal to reload the entire tab content instance each time, but I just wanna make sure that I know the basics of what I'm trying to achieve. Any assistance is greatly appreciated.
videos in sprite containers not displaying?   (147 Views)
Hi all! I'm generating a bunch of thumbnail videos that should have various mouse reactions. Since the Video class doesn't dispatch mouse events I put the videos in sprite containers: Code: private function makeVideo(src:String):void { videos.push(new Object); var this_index:uint = videos.length - 1; var this_container:Sprite = videos[this_index].video = new Sprite(); var this_stream:NetStream = videos[this_index].stream = new NetStream(connection); this_stream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler); this_stream.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler); var this_video:Video = new Video(); this_video.attachNetStream(this_stream); this_video.smoothing = true; resizeThumb(this_container, thumb_width_max, thumb_height_max); this_container.x = this_index * (thumb_width_max + 50) + 50; this_container.y = 200; this_container.addEventListener(MouseEvent.MOUSE_DOWN, vidClick); this_container.addEventListener(MouseEvent.MOUSE_OVER, vidOver); this_container.addEventListener(MouseEvent.MOUSE_OUT, vidOut); videos[this_index].src = src;, 2); this_container.addChild(this_video); addChild(this_container); } ...but nothing shows up, and mouse movements over the area where they should be don't generate any events. What am I missing I had the videos displaying fine before I tried putting them in containers, they just couldn't react to mouse events. For the record, the videos[] array is just a reference array of objects. I don't think that's interfering, but ...
Adding one textfield to two sprites problem   (181 Views)
Hi all, I'm having a bit of trouble with the SimpleButton object. I have a menu screen which contains nine buttons, so I built a createMenuButton function to save some time. The createMenuButton function creates two sprites, with the up and over images, and adds a TextField as well. Here's my createMenuButton function: PHP Code: privatefunctioncreateMenuButton(Text:String,xPos:int,yPos:int,onClickFunction:Function):SimpleButton{ //createthetextFieldforthemiddleofthebutton: vartf:TextField=newTextField(); tf.embedFonts=true; tf.defaultTextFormat=Fonts.DUEDATE_20; tf.width=230; tf.height=100; tf.text=Text; tf.border=true; varupSprite:Sprite=newSprite(); upSprite.addChild(newBUTTON_UP_IMG()); upSprite.addChild(tf); varoverSprite:Sprite=newSprite(); overSprite.addChild(newBUTTON_OVER_IMG()); overSprite.addChild(tf); varbtn:SimpleButton=newSimpleButton(upSprite,overSprite,upSprite,upSprite); btn.addEventListener(MouseEvent.CLICK,onClickFunction); btn.x=xPos; btn.y=yPos; returnbtn; } So, What I want is for the up state to show the up image, with the text field on top, and the over state to show the over state image, with the textfield on top as well. What's actually happening is the textField is only going on to the over state, even though I add it to both states with addchild. If I don't add the text field to the over state, it displays fine on the up state, but when I add it to the over state, it disappears from the up state sprite. Any ideas how I can fix this, or why it's happening
[CS5] stroke affects total sprite size?, that's new?   (119 Views)
Hi.. If you create a movieclip, inside create sprite that is 100x100 px..and add a stroke around it.. If you trace _width of that movieclip it's 101x101 px. ..i'm pretty sure this was not the case in earlier versions of flash.. Can someone confirm
Importing 255+ sprite images...must be an easier way!   (201 Views)
! I'm trying to import 259 images *sprite based* due to the fact that Flash CS3 Pro won't import uncompressed .avi videos without making them look terrible and very low quality. I'm designing flash that will run on a local computer and will require no downloading but all the import options are download based thus limiting the quality of video to 9999kbps :\ So i'm forced to sprite my video and manually put ONE sprite per frame so form high quality animation.... PLEASE tell me there's a faster way for me to import 259 sprites into 259 frames than to go in ONE BY ONE and move them to the box manually lol You'd think that for such high end software there would be an option to high light all your sprites and move them to the frames in order :\
smoothly moving large sprites   (128 Views)
Hi! I've got a problem in actionscript3 with moving large DisplayObject-s. When the size of the DisplayObject is quite large (more than screen size) the movement loses smoothness and it looks like the object starts jumping forward and backward, which overall looks very unpleasant. Does anybody know the way to fix that I am trying to make a sort of a race game, where I need to move the background sprite to make the illusion of movement. Code: private var obj:Sprite; private var canvas:Sprite; // determines spatial spread of circles private var size:int = 1000; public function ViewSpurTest3() { stage.frameRate = 31; stage.scaleMode = StageScaleMode.NO_SCALE; stage.align = StageAlign.TOP_LEFT; // make the static circle to compare with // moving circles obj = new Sprite(); with ({ beginFill(0xFF00FF00); drawCircle(0,0,10); endFill(); } // draw a lot of circles on canvas canvas = new Sprite(); var i:int; with ({ beginFill(0xFFFF0000); for(i = 0; i= 200) canvas.x -= 800; }
maximum stage and sprite size   (128 Views)
folks, I'm making an action game and I'd like to know what should be the maximum size of the stage (mine is 660 x 500). Also I'd like to know how big a game-sprite should be. Currently my biggest sprites have a size of 128 x 128 and I read somewhere on the internet that you should not make it bigger because of performance issues. If you want to make e.g. big explosions with shockwaves even 128 x 128 does not look very big. What's the maximum size I can definitely use for sprites I cannot find any real solution about this so I appreciate every hint I can get because this topic makes me a little bit nervous.
movieclip not sprite   (118 Views)
, i am hoping someone can give me a clue on how to swap the sprite instance for a movieclip via the document class. this a boid example from soulwire, and works great but i want to use a library clip instead of the vector sprite - for arguments sake - the library clip would be "boid"... PHP Code: package {; importflash.display.Sprite;; importflash.geom.Vector3D; /** *Demo */ [SWF(width="800",height="600",backgroundColor="#FFFFFF",frameRate="31")] publicclassAbstractDemoextendsSprite { protectedvar_boids:Vector.=newVector.(); protectedvar_boidHolder:Sprite; protectedvar_config:Object={ minForce:3.0, maxForce:6.0, minSpeed:6.0, maxSpeed:12.0, minWanderDistance:10.0, maxWanderDistance:100.0, minWanderRadius:5.0, maxWanderRadius:20.0, minWanderStep:0.1, maxWanderStep:0.9, boundsRadius:250, numBoids:120 }; publicfunctionAbstractDemo() { _boidHolder=addChild(newSprite())asSprite; addEventListener(Event.ADDED_TO_STAGE,onAddedToStage); } protectedfunctioninit():void { //Init } protectedfunctioncreateBoid():Boid { varboid:Boid=newBoid(); setProperties(boid); boid.renderData=boid.createDebugShape(000000,4.0,2.0); _boids.push(boid); _boidHolder.addChild(boid.renderData); returnboid; } protectedfunctioncreateBoids(count:int):void { for(vari:int=0;i
cant pass sprite   (247 Views)
when I pass a sprite variable to a class to place images in it the spite doesnt display all the image as it should. It is unstable. Code: canvas1=new Sprite(); addChild(canvas1); mybackground=new ClassBackGroundtile(canvas1); canvas1.addChild(mybackground); /////////// ///////////////////////// classbackgroundTile private var canvas1:Sprite; public function ClassBackGroundTile(ccanvas1:Sprite...) { canvas1=ccanvas1; ..... //addChild(img1); canvas1.addChild(img1); //this causes the problem
AS3: Get sprite at particular coordinates   (139 Views)
Is it possible to test if a sprite (or movieclip) is located at a set of coordinates If a sprite is located at the coordinates, can you retrieve it's instance name If no sprite is located at coordinates, can you output a value Sorry for the barrage of questions. -C-
clarification of sprite.clear() usage   (128 Views)
My understanding is that calling clear() on a sprite object will wipe the screen clear of previous drawings for the next draw. But my code below renders nothing to the screen when there is a clear() called. What am I doing wrong Code: var sp:Sprite = new Sprite(); addChild(sp); var g:Graphics =; g.lineStyle(5,0x000000); sp.addEventListener(Event.ENTER_FRAME, draw_line); function draw_line(event:Event) { g.clear(); g.moveTo(0,0); g.lineTo(mouseX, mouseY); }