A Flash Developer Resource Site

Results 1 to 14 of 14

Thread: [RESOLVED] Image not displaying?

  1. #1
    Lunatic
    Join Date
    Nov 2002
    Location
    AS3 Forum
    Posts
    342

    resolved [RESOLVED] Image not displaying?

    Alright, another problem. Made this class that loads pictures for me. You pass on the picture URL, the object that the picture needs to be added to and a function to do after the image is loaded.

    Here is the class:

    Code:
    import flash.display.Sprite;
    	import flash.events.*;
    	import flash.display.Loader;
    	import flash.net.URLRequest;
    	
    	
    	public class PicLoader	
    	{
    		private var s:Loader;
    		private var path:String;
    		private var addTo:Object;
    		private var func:Function;
    				
    		public function loadPic(p:String, aT:Object, f:Function)
    		{
    			path = p;
    			addTo = aT;
    			func = f;
    
    			s = new Loader();
    			s.contentLoaderInfo.addEventListener(Event.COMPLETE, returnSprite);
    			s.load(new URLRequest(path));			
    		}
    		
    		private function returnSprite(e:Event):void
    		{
    			trace("adding pic");
    			addTo.addChild(e.target.content);
    			func();
    			s = null;
    		}
    	}
    Now I am using this class to load a background picture and add it to an object called BG (which is a class that extends the Sprite class).

    Here is the code for that BG class (shortened to the important stuff):

    Code:
    import flash.display.MovieClip;
    	import flash.display.Sprite;
    	import flash.display.Stage;
    	import flash.events.*;
    	import flash.utils.Timer;
    	import fl.transitions.Tween;
    	import fl.transitions.easing.*;
    	import fl.transitions.TweenEvent;
    	
    	public class BG extends Sprite
    	{	
    		public var easeInBg:Tween;
    		public var timeFade:Timer;
    		
    		public function BG()
    		{
    			if (!stage)
    			{
    				addEventListener(Event.ADDED_TO_STAGE, init);
    			} else {
    				init(null);
    			}
    		}
    		
    		private function init(e:Event):void
    		{
    			removeEventListener(Event.ADDED_TO_STAGE, init);
    			buildBG();
    		}
    		
    		private function buildBG():void
    		{
    			this.width = stage.stageWidth;
    			this.height = stage.stageHeight;
    			this.alpha = 0;
    		}
    		
    		public function displayBG():void
    		{
    			resizeBG();
    			easeInBg = new Tween(this, "alpha", Strong.easeIn, 0, 1, 1, true );
    		}
    		
    	}
    Now this is how I add the picture to that BG object:

    Code:
    			bg = new BG();
    			addChild(bg);
    			
    			pL = new PicLoader();
    			pL.loadPic("http://www.mmphotography.ca/bg_home.jpg", bg, bg.displayBG);
    The compiler doesn't throw any errors, I can trace the BG object having a children after adding the picture to it (trace(bg.numChildren) returns 1) yet it won't show it! What is wrong?!

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I don't see the problem in the code you posted. What does bg.alpha trace as?

    In PicLoader, addTo should be typed as DisplayObjectContainer since you know it must have an addChild method. But that's not causing your issue here.

  3. #3
    Lunatic
    Join Date
    Nov 2002
    Location
    AS3 Forum
    Posts
    342
    I commented out the tween and the this.alpha = 0 part in the builder and it still didn't show up.

    I'm completly baffled right now.

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    What does resizeBG() do?

    You might have to post your actual code instead of this stuff you believe is equivalent.

  5. #5
    Lunatic
    Join Date
    Nov 2002
    Location
    AS3 Forum
    Posts
    342
    Code:
    package com.berubegraphics.martinemartell
    {
    	import flash.display.MovieClip;
    	import flash.display.Sprite;
    	import flash.display.Stage;
    	import flash.events.*;
    	import flash.utils.Timer;
    	import fl.transitions.Tween;
    	import fl.transitions.easing.*;
    	import fl.transitions.TweenEvent;
    	
    	public class BG extends Sprite
    	{	
    		public var easeInBg:Tween;
    		public var timeFade:Timer;
    		
    		public function BG()
    		{
    			if (!stage)
    			{
    				addEventListener(Event.ADDED_TO_STAGE, init);
    			} else {
    				init(null);
    			}
    		}
    		
    		private function init(e:Event):void
    		{
    			removeEventListener(Event.ADDED_TO_STAGE, init);
    			buildBG();
    		}
    		
    		private function buildBG():void
    		{
    			this.width = stage.stageWidth;
    			this.height = stage.stageHeight;
    			//this.alpha = 0;
    		}
    		
    		public function clearBG():void
    		{	
    			this.removeChildAt(0);
    		}
    		
    		public function displayBG():void
    		{
    			//resizeBG();
    			//easeInBg = new Tween(this, "alpha", Strong.easeIn, 0, 1, 1, true );
    			
    			if ((parent as Main).buildingSite == true) 
    			{
    				timeFade = new Timer(1500, 1);
    				timeFade.addEventListener(TimerEvent.TIMER_COMPLETE, buildNav);
    				timeFade.start();
    			}
    		}
    		
    		public function buildNav(e:TimerEvent):void
    		{
    			dispatchEvent(new Event("buildNavBar", true));
    		}
    			
    		public function resizeBG()
    		{
    			this.getChildAt(0).scaleX = this.getChildAt(0).scaleY = 1;
    			
    			if ((stage.stageHeight / stage.stageWidth) <= this.getChildAt(0).height / this.getChildAt(0).width)
    			{
    				this.getChildAt(0).width = stage.stageWidth;
    				this.getChildAt(0).scaleY = this.getChildAt(0).scaleX;
    			} else {
    				this.getChildAt(0).height = stage.stageHeight;
    				this.getChildAt(0).scaleX = this.getChildAt(0).scaleY;
    			}
    			
    			if (this.getChildAt(0).width < stage.stageWidth)
    			{
    				this.getChildAt(0).width = stage.stageWidth;
    				this.getChildAt(0).scaleY = this.getChildAt(0).scaleX;
    			}
    		}
    		
    		
    	}
    	
    }
    This is the full class. I commented out the resizeBG() and it didn't work.

  6. #6
    Lunatic
    Join Date
    Nov 2002
    Location
    AS3 Forum
    Posts
    342
    Also just tried tracing the width and height of "this.getChildAt(0)" and I get 1680x1050 (which is my picture size). So it's there for sure.

  7. #7
    Lunatic
    Join Date
    Nov 2002
    Location
    AS3 Forum
    Posts
    342
    Just ran this:


    trace(this.getChildAt(0).width + " " + this.getChildAt(0).height + " " + this.getChildAt(0).alpha + " " + this.alpha);

    and got this:

    1680 1050 1 1

    Everything seems like it's in order. For some reason it's not displaying

  8. #8
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Can you tell if all your functions are executing as you expect? I still don't see the problem, but you should be able to step through and see if things are at least executing correctly.

    Also, is bg or bg's content placed offstage somehow? Trace all the values you can think of to see why it's not visible in the end (visible, alpha, x, y, width, height of both bg and bg's content). Once you figure out why it's not showing up, you can figure out what code is screwing that up.

    Instead of using getChildAt(0) so many times, you can save the result of calling it the first time and just use that variable.

  9. #9
    Lunatic
    Join Date
    Nov 2002
    Location
    AS3 Forum
    Posts
    342
    Quote Originally Posted by 5TonsOfFlax View Post
    Can you tell if all your functions are executing as you expect? I still don't see the problem, but you should be able to step through and see if things are at least executing correctly.

    Also, is bg or bg's content placed offstage somehow? Trace all the values you can think of to see why it's not visible in the end (visible, alpha, x, y, width, height of both bg and bg's content). Once you figure out why it's not showing up, you can figure out what code is screwing that up.

    Instead of using getChildAt(0) so many times, you can save the result of calling it the first time and just use that variable.
    Deadly! Figured it out. When I called the buildBG() function turns out the stage.stageWidth at that point was 0 and same for stageHeight 0. So I ended up just removing the width and height setting lines and it just adjusts to the picture now.

    As for the getChildAt(0) part, how would you do this?

    Is it like this:

    var pic:Object = this.getChildAt(0);

    Or should it be defined as a sprite?
    Last edited by Beathoven; 08-23-2010 at 04:48 PM.

  10. #10
    Lunatic
    Join Date
    Nov 2002
    Location
    AS3 Forum
    Posts
    342
    Am assuming I should also store the width and height of the stage too so it's not calling it 50 times?

  11. #11
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Something like that. But instead of Object, use DisplayObject because getChildAt returns a DisplayObject, and you want to deal with properties that a DisplayObject has.

    You could cache stagewidth and stageheight in local properties, but I wouldn't bother. The difference between local variable access and property access is not worth it unless you are dealing with thousands of accesses each frame. You could do it just because it's easier to write "w" than "stage.stageWidth", but whether that makes it more readable is up to you.

    for getChildAt, the local variable access is much faster than function evaluation, but that again would not matter unless you're doing thousands of accesses. But "pic" is definitely more readable than "getChildAt(0)". So make it easier on yourself and the vm.

  12. #12
    Lunatic
    Join Date
    Nov 2002
    Location
    AS3 Forum
    Posts
    342
    Thanks a lot man.

    Where do I send the beer?

  13. #13
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Not necessary

    But, if you're in the Austin area, come to the flash meetup Wednesday at SpringBox. I'll be giving a short talk about computer vision in flash, and there will be provided beer and snacks.

  14. #14
    Lunatic
    Join Date
    Nov 2002
    Location
    AS3 Forum
    Posts
    342
    I'm more in the Alberta area, north of the US lol.

    But thanks for the invite.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center