Image is null when used in a function [SOLVED]


(Zouhair Elamrani Abou Elassad) #1

Ok, i think i’m missing something here, i’ve trying to do some refactoring with my code but something is not working right, this is what t had :

                    _bottomArt = new Image(ART);
		_bottomArt .centerOO();
		_bottomArt .smooth = true;
		_bottomArt .x = ART_WIDTH / 2;
		_bottomArt .y = ART_HEIGHT;

i created this function :

    private function initART(image:Image):void 
	{
		image = new Image(ART);
		if (image != null) {
			
                            trace("image is not null");
                            image.centerOO();
			image.smooth = true;
			image.x = ART_WIDTH / 2;
			image.y = ART_HEIGHT;
		}
		if (_bottomArt == null) {
			trace("_bottomArt is null");
		}
	} 

i call it while passing the parameter :

   initMiddleGears(_bottomArt);

i get the both traces :

      trace("image is not null");
      trace("_bottomArt is null");

am i missing something ?


(Martí Angelats i Ribera) #2

Where do you have the first part?


(Jean) #3

Well, its because passing _bottomArt means giving “a copy” of the object I believe.

The image var then just changes the local function “copy” of _bottomArt.

Make the initART return image or just acess directly _bottomArt


(Martí Angelats i Ribera) #4

I think it is becouse of the way AS3 works. Just return the image and assing it to the variable.

PS: This doesn’t work becouse of the “new Image(ART)” part. You are creating a new Image and making img point to that, but _bottomArt is still pointing the old one (in this case null).


(Zouhair Elamrani Abou Elassad) #5

You guys are right, it’s weird :p, i changed my fonction to the following:

private function initART():Image
{
	var image:Image = new Image(ART);

                    image.centerOO();
		image.smooth = true;
		image.x = ART_WIDTH / 2;
		image.y = ART_HEIGHT;
         return image;

}

and then call it this way :

_bottomArt = initArt();