Accessing Entities through World


(Noah) #1

I’ve been trying to use the world I am in to access and change variables in my entities.

I noticed there was a tutorial on the site already but the following line of code confuses me:

var player = world.getInstance("player") as Player;

When I try putting this line into my code I get an error. And when I try changing “world” to the name of my current world I still get an error. I do have the code to name my entity the name that is in the paramater but I am still getting errors.

Thank you for any help.


(Jacob Albano) #2

Where is this code? Is it in your world class, or an entity? What type of error are you getting – runtime or compiler – and what’s the exact message? There’s nothing wrong with that line of code in your post so without more information all we can do is guess what the issue is.


(Noah) #3

Here’s the code of my world which is called TestWorld1:

package  {

import net.flashpunk.World;
import net.flashpunk.FP;
import net.flashpunk.utils.*;

public class TestWorld1 extends World{

	public function TestWorld1() {
		add(new Player(50,50));
	}
	
	override public function update():void
	{
		var player = world.getInstance("player") as Player;
	}
}

And here is the code for my Entity which is called Player:

package {

import net.flashpunk.Entity;
import net.flashpunk.utils.*;
import net.flashpunk.Graphic;
import net.flashpunk.graphics.Image;
import net.flashpunk.graphics.Spritemap;

public class Player extends Entity{
	
	[Embed(source = 'assets/hatman.png')]
	private const HATMAN:Class;

	public var sprHatman:Spritemap = new Spritemap(HATMAN, 30, 62);

	public function Player(x:Number,y:Number) {
		super(x,y);
		sprHatman.add("stand",[0], 20, true);
		sprHatman.add("walk",[1,2,3], 20, true);
		
		graphic = sprHatman;
		sprHatman.play('stand');
		name = 'player';
	}

}

And here is the error I am getting:

/Users/npollema16/Desktop/My Stuff/Flash Stuff/Games/To bed/TestWorld1.as, Line 9 1120: Access of undefined property world.


(rostok) #4

The World class itself has no world property (Entity does). So in order to get the player use just getInstance("player"). Also consider moving this code outside of update.


(Martí Angelats i Ribera) #5

As @rostok said you don’t need the world part. The code is:

override public function update():void
{
	var player = getInstance("player") as Player;
}

But if you want to get the player instanceevery frame don’t do this. It is not efficient.

What you want to do instead is to have a variable and save the player there (corrected by @jacobalbano in the next comment [thank you]).

public var player:Player;
public function TestWorld1() {
	player = add(new Player(50,50));
}

(Jacob Albano) #6
add(player = new Player(50, 50));

Your way won’t work without casting it.


(Noah) #7

Deleting world solved my problem, however after that a similar problem has risen.

player.x += 5;

This line is getting me this error:

TypeError: Error #1009: Cannot access a property or method of a null object reference. at net.flashpunk::World/getInstance() at TestWorld1() at Main()

I’m assuming that it isn’t possible to change variables like this through this method. Is there an easy way to have my World class effect the x or y values of an entity?


(Jean) #8

var player = world.getInstance("player") as Player;

I believe that getInstance is case sensitive, so change that to

var player = world.getInstance("Player") as Player;

And it will avoid your error.


(rostok) #9

I would strongly suggest adding player variable to world and setting it up when the player entity is actually added. Don’t know how others do it but I personally use getInstance() for dynamic entities. Such entities can but don’t have to be in world. Therefore it would be good to see if actually getInstance() returned null.


(Jacob Albano) #10

Nope. He sets the name property on his Player class to “player”, so lowercase ‘p’ is correct. It doesn’t use the class name.


(Jacob Albano) #11

Your player instance is null, so attempting to do anything with it will fail. That line (where you modify player.x is being run 1) before the player is created, or 2) before the player is added to the world (so getInstance() will have no effect).

Save your player instance in a variable when you create it and you won’t run into this particular error.