Jump to content

Problem with loading asset in Pixi.js


neonwarge04
 Share

Recommended Posts

I can't seem to load a sprite without having exceptions. I had my Sprite getting exceptions since it is receiving undefine. One thing for sure this exist because it receives a texture which most probably cannot be located.

I did the simple loading of texture in a separate js file like this. I am not fluent in JS by the way.

 

function SpaceShip(filename){	this.mSpriteFile = filename;	this.mFireRate = 0.25;		this.mCollisionRadius = 42.0;	this.mIsMarkedDestroyed;		this.mPixelPosition = {x : 0 , y : 0};		this.mOnPixelPositionChanged = function(lastPosition , newPosition)	{		//	}		this.mTexture = PIXI.Texture.fromImage(filename);		this.mSprite = PIXI.Sprite(this.mTexture);	this.mSprite.anchor.x = 0.5;	this.mSprite.anchor.y = 0.5;}

Here is how my directory looks like

|-_assets

|    - spaceship.jpg <--- Asset I wanted to load

|-src

|    - Entities.class.js

|-core

|    -pixi.js 

|-index.html

 

I don't know what causing the sprite to receive an exception stating it cannot define 'anchor'. On debugger this.mSprite is undefine.

I assume this is because the texture wasn't there in the first place. I also check the state of mTexture and found out the width and the height of the loaded texture is 0. I pretty sure none was loaded which lead me to believe it can't access the folder from another.

Any ideas?

Thanks!

 

Link to comment
Share on other sites

@bubamara I've been fixing this for three hours >_> Thank you very much!

But I changed my code since and I still get a black screen.

index.html
 

<!DOCTYPE HTML><html>	<head>		<title>Exercise 1</title>		<script src="core/pixi.js"></script>		<script src="src/example/Person.class.js"></script>		<script src="src/Entities.class.js"></script>	</head>		<body>		<script>								var renderer = PIXI.autoDetectRenderer(800 , 600, {backgroundColor : 0x000000});			document.body.appendChild(renderer.view);							var startingStage = new PIXI.Container();						var spaceshipTexture = PIXI.Texture.fromImage('_assets/textures/spaceship.png');			var enemyshipTexture = PIXI.Texture.fromImage('_assets/textures/enemyship.png');						var spaceshipSprite  = new PIXI.Sprite(spaceshipTexture);			var enemyshipSprite  = new PIXI.Sprite(enemyshipSprite);						var spaceship = new SpaceShip(spaceshipSprite);			spaceship.setPixelPosition({x : 400 , y : 300});						startingStage.addChild(spaceship.getSprite());							animate();							function animate()			{				//spaceship.getSprite().rotation += 0.1;				renderer.render(startingStage);			}						</script>	</body>	</html>

Entities.class.js

 

function SpaceShip(sprite){	this.mSprite = sprite;	this.mFireRate = 0.25;	this.mCollisionRadius = 42.0;	this.mIsMarkedDestroyed;		this.mPixelPosition = {x : 0 , y : 0};		this.mOnPixelPositionChanged = function(lastPosition , newPosition)	{		//	}}SpaceShip.prototype.markDestroyed = function(){	this.mIsMarkedDestroyed = true;}SpaceShip.prototype.isMarkedDestroyed = function(){	return this.mIsMarkedDestroyed;}SpaceShip.prototype.setOnPositionChanged = function(callback){	this.mOnPixelPositionChanged = callback;}SpaceShip.prototype.setPixelPosition = function (position){	var previousPosition = this.mPixelPosition;		this.mPixelPosition.x = position.x;	this.mPixelPosition.y = position.y;		this.mOnPixelPositionChanged(previousPosition , this.mPixelPosition);			this.mSprite.position = this.mPixelPosition.x;	this.mSprite.position = this.mPixelPosition.y;}SpaceShip.prototype.getSprite = function(){	return this.mSprite;}
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...