Change World - new Player?


(Bartłomiej Kalemba) #1

Hi Everyone! I have a little problem with changing World’s:

FP.world = new MyWorld2(this);

this - is the Player class (this code is in Player.update() ) In constructor method of another World i try to:

public function MyWorld2(myPlayer:Player)
	{			
		add(myPlayer);
	}

but then player.update() method isn’t invoke. I want to save for example HP data and so other… Do I have to:

 add(new Player()); 

and then manualy update new player with datas from old instance?

Also when i switching World were I generate for exaple Rectangle 6000x1000 tileds for background: flash player don’t responding… Did I sth wrong?

Really sorry for my English…


(Jacob Albano) #2

You may have to remove the player from the original world in order to add him to a new one

There isn’t nearly enough information in your second question in order for us to help with that issue…


(Bartłomiej Kalemba) #3

My bad… to less code, so :

package

{ import flash.display.Shape; import flash.utils.ByteArray; import net.flashpunk.Entity; import net.flashpunk.graphics.Tilemap; import net.flashpunk.masks.Grid; import net.flashpunk.FP;

public class Level2 extends Entity
{
	
	private var _tiles2:Tilemap;
	private var _grid:Grid;
	
	public function Level2()
	{
		
		_tiles2 = new Tilemap(Assets.SPRITE_TILESET, FP.width, FP.height, 128, 68);
		_tiles2.x = -64;
		_tiles2.y = -32;
		_tiles2.setRect(0, 1, FP.width, FP.height, 0);
		graphic = _tiles2;
		layer = 99999999;
		
		//_grid = new Grid(640, 480, 128, 68, 0, 0);
		//mask = _grid;
		
		type = "level";
	}

}

}

This is code for my entity which I add to new World, but now I think, that maybe better is load an whole PNG as background? Also render only few tiles and then:

  • world will be faster switching
  • create sth like fog of war and in this time load the rest of the tiles?

(Ultima2876) #4

For setRect, the width and height are in tiles. You’re setting FP.width tiles when the tilemap is only FP.width pixels large. FP.width tiles, if the tiles are 128x64, is going to be massive anyway. But that aside, if you just want a screen of tiles, use this:

_tiles2.setRect(0, 0, Math.ceil(FP.width / 128), Math.ceil(FP.height / 64), 0);

If you want to fill your tilemap, you can do this:

_tiles2.setRect(0, 0, _tiles2.columns, _tiles2.rows, 0);

Also (maybe someone else can confirm) I’m pretty sure tilemaps are cleared to 0-index tiles in their constructor so using setRect to set them to 0 isn’t necessary!


(Bartłomiej Kalemba) #5

OMG… that is so true. Now everything is loading in just a second! No matter if I put 10000 pixels!

Thanks for that!


(Ultima2876) #6

Awesome :slight_smile: Glad to hear it’s behaving now!


(Bartłomiej Kalemba) #7

Is there some advantages for using more tiles but smaller dimension?


(Ultima2876) #8

In terms of performance, not really (unless you’re changing a lot of tiles very often, eg in a game that has heavily animated tilesets, in which case the overhead for changing lots of small tiles will be more expensive). In terms of flexibility in your level designs, however, having smaller tiles means you can have more complex arrangements provided your tilesets are designed that way.

Once the tiles are set in the tilemap they are drawn to an internal Canvas, meaning that there’s no rendering performance difference at all between tile sizes.


(Bartłomiej Kalemba) #9

Ok, now I know everything what I want (for now ;p). Thanks for big explain me everything what I asked about. That was really helpfull for me! :smile:


(Bartłomiej Kalemba) #10

About my first question here: In current world:

FP.world.remove(player);
		FP.world = new WorldEnd(player);

And new World constructor look like this:

public function WorldEnd(oldPlayer:Player) 
	{
		trace(oldPlayer);
		this.player = oldPlayer;
		add(this.player);
		super();
		
	}

But then in output I get my trace and:

[Fault] exception, information=TypeError: Error #1009: Cannot access a property or method of a null object reference. Fault, World.as:134

(Jacob Albano) #11

You’re probably passing null to add() or remove() at some point.


(Bartłomiej Kalemba) #12

Half solved. Tips: NEVER add() in constructor. Now I just assign:

this.player = oldPlayer;

And add() in begin method(), but FP console says: 0 Entities and I don’t see my player. I must say that I reset his x and y to begin position.


(Jacob Albano) #13

Adding in the world’s constructor shouldn’t be a problem. I do it fairly often.

Resetting the position of the player shouldn’t be a problem either…I really have no idea what the problem could be. Can you upload your project somewhere?


(Ultima2876) #14

Make sure your world.update() function override contains a super.update() call.


(Bartłomiej Kalemba) #15

OK, I have no idea what happened, but I go out from my home. Now I’m back and everything works fine… weird but working :smiley:


(Ultima2876) #16

@jacobalbano snuck into your house and fixed your code for you ;D