Create hero/enemies/obstacles within the map?

This is a pretty newbie question but I am unsure how to do this. When I worked with Lime, I would pick any tile in tiled and give it a property, such as: “isHero”. (or “isEnemy” or “isSuperfish”, etc). Then I’d just write this code:

[lua]
local onPlayerProperty = function(property, type, object)

    local hero = object.sprite

end

map:addPropertyListener(“isHero”, onPlayerProperty)
[/lua]

From this point the var “hero” points to a display object I can do anything I want with, as if I created it from within the code. What will be the right way doing this in MTE?

I have to admit, your question threw me for a little bit. After giving it some thought and looking into Lime I now see that it comes from some of the differences between how Lime and MTE operate. 

Long story short, MTE does not have an equivalent to Lime’s addPropertyListener map event listener, but now that I’ve given it thought I’ve come to realize how very useful it could be. So, I fully intend to add something similar. If possible I’ll have it ready for next week’s MTE update.

In the past my assumption in designing MTE was that developers like yourself would rather create sprites on their own, and then add those sprites to MTE using the addSprite() function. I envisioned developers using Tiled Objects as spawn points, adding them to the map in Tiled and retrieving them by name using getObject(). For example:

--LOAD SPAWN local playerSpawn = mte.getObject({name = "playerSpawn"}) --CREATE PLAYER SPRITE ------------------------------------------------------------ local spriteSheet = graphics.newImageSheet("spriteSheet.png", {width = 32, height = 32, numFrames = 96}) local sequenceData = { {name = "up", sheet = spriteSheet, frames = {85, 86}, time = 400, loopCount = 0}, {name = "down", sheet = spriteSheet, frames = {49, 50}, time = 400, loopCount = 0}, {name = "left", sheet = spriteSheet, frames = {61, 62}, time = 400, loopCount = 0}, {name = "right", sheet = spriteSheet, frames = {73, 74}, time = 400, loopCount = 0} } local player = display.newSprite(spriteSheet, sequenceData) local setup = { kind = "sprite", layer = playerSpawn[1].layer, levelPosX = playerSpawn[1].x, levelPosY = playerSpawn[1].y, levelWidth = 32, levelHeight = 32 } mte.addSprite(player, setup) mte.setCameraFocus(player)

You could store all the parameters you need in the Tiled Object’s properties just as you would in a tile’s properties. The resulting code is not much different than the Lime equivalent. The primary difference is that the Lime equivalent is triggered as Lime creates the world, while the MTE code is executed by you, anytime after MTE loads the map.

You can retrieve the display objects of individual tiles using getTileObj(), however it is important to keep in mind that whereas Lime builds the entire map, MTE only builds that part of the map visible onscreen. This is the culling mechanism responsible for MTE’s superior speed. Offscreen tiles don’t exist in the same sense as onscreen tiles, they do not have a display object for you to retrieve. 

Feel free to ask if you have other questions or need further explanation, and keep in mind that the Million Tile Engine is a Beta product subject to change and improvement as customer feedback comes in. 

I understand MTE works differently by only maintaining Display Objects that are actually on screen, and thus much more efficient than lime. Thus, in MTE instead of having a property listener that runs only once like in Lime, there should be a listener that would run whenever a new Display Object is created, giving you full control over that sprite.

Also, since certain objects such as enemies are not static, you should be able to change the original object’s data once it goes off screen. For example, if you killed an enemy, you wouldnt want to spawn it again the next time you come back to the same spot. Or if the enemy moved somewhere, or got hit and has less HP, next time it will spawn in the new location with less HP, etc.

Hey

I have used the above code for spawning enemy and player locations and have run into an interesting issue. I think I am missing something simple but not quite sure.

My code is the same logic as the above but when I call the following function

mte.addSprite(player, setup)

The actual levelPosX and LevelPosY coordinates of the player are not correct.

For instance instead of player appearing at the loc of the playerspawn point in the Tile editor the player will appear several tiles away in the x and y direction.

Any ideas?

Thanks

Just to add

I check the levelPosX and y of the player before and after addsprite is called and the values are different

It’d be helpful if you posted some images of the problem and the code you’re using. Some things to look into off the top of my head; in my example above, playerSpawn is a Tiled Object, it is not a tile or a sprite. The x and y parameters of Tiled Objects are in relation to the scale of the map, while the x and y positions of tiles and sprites are in relation to your blockScale. When you check the position of a sprites you should use sprite.getLevelPosX and sprite.getLevelPosY. 

But, like I said, I’d need to see code at least in order to be of any real help.

Well that actually pointed us in the right direction. :slight_smile:

The map was incorrectly made in the Tiled Editor. When the map was made the size of tiles were not correctly made, thus causing an issue with incorrect tile sizes, etc, thus a train wreck of problem finally showed up in the spawning of players and enemies.
Everything works great now. Our little ship moves around and shoots the enemies now.

I will admit, I am the one who screwed up the map. That is what happens when an engineer tries to make his own test art. :wacko:
 

I have to admit, your question threw me for a little bit. After giving it some thought and looking into Lime I now see that it comes from some of the differences between how Lime and MTE operate. 

Long story short, MTE does not have an equivalent to Lime’s addPropertyListener map event listener, but now that I’ve given it thought I’ve come to realize how very useful it could be. So, I fully intend to add something similar. If possible I’ll have it ready for next week’s MTE update.

In the past my assumption in designing MTE was that developers like yourself would rather create sprites on their own, and then add those sprites to MTE using the addSprite() function. I envisioned developers using Tiled Objects as spawn points, adding them to the map in Tiled and retrieving them by name using getObject(). For example:

--LOAD SPAWN local playerSpawn = mte.getObject({name = "playerSpawn"}) --CREATE PLAYER SPRITE ------------------------------------------------------------ local spriteSheet = graphics.newImageSheet("spriteSheet.png", {width = 32, height = 32, numFrames = 96}) local sequenceData = { {name = "up", sheet = spriteSheet, frames = {85, 86}, time = 400, loopCount = 0}, {name = "down", sheet = spriteSheet, frames = {49, 50}, time = 400, loopCount = 0}, {name = "left", sheet = spriteSheet, frames = {61, 62}, time = 400, loopCount = 0}, {name = "right", sheet = spriteSheet, frames = {73, 74}, time = 400, loopCount = 0} } local player = display.newSprite(spriteSheet, sequenceData) local setup = { kind = "sprite", layer = playerSpawn[1].layer, levelPosX = playerSpawn[1].x, levelPosY = playerSpawn[1].y, levelWidth = 32, levelHeight = 32 } mte.addSprite(player, setup) mte.setCameraFocus(player)

You could store all the parameters you need in the Tiled Object’s properties just as you would in a tile’s properties. The resulting code is not much different than the Lime equivalent. The primary difference is that the Lime equivalent is triggered as Lime creates the world, while the MTE code is executed by you, anytime after MTE loads the map.

You can retrieve the display objects of individual tiles using getTileObj(), however it is important to keep in mind that whereas Lime builds the entire map, MTE only builds that part of the map visible onscreen. This is the culling mechanism responsible for MTE’s superior speed. Offscreen tiles don’t exist in the same sense as onscreen tiles, they do not have a display object for you to retrieve. 

Feel free to ask if you have other questions or need further explanation, and keep in mind that the Million Tile Engine is a Beta product subject to change and improvement as customer feedback comes in. 

I understand MTE works differently by only maintaining Display Objects that are actually on screen, and thus much more efficient than lime. Thus, in MTE instead of having a property listener that runs only once like in Lime, there should be a listener that would run whenever a new Display Object is created, giving you full control over that sprite.

Also, since certain objects such as enemies are not static, you should be able to change the original object’s data once it goes off screen. For example, if you killed an enemy, you wouldnt want to spawn it again the next time you come back to the same spot. Or if the enemy moved somewhere, or got hit and has less HP, next time it will spawn in the new location with less HP, etc.

Hey

I have used the above code for spawning enemy and player locations and have run into an interesting issue. I think I am missing something simple but not quite sure.

My code is the same logic as the above but when I call the following function

mte.addSprite(player, setup)

The actual levelPosX and LevelPosY coordinates of the player are not correct.

For instance instead of player appearing at the loc of the playerspawn point in the Tile editor the player will appear several tiles away in the x and y direction.

Any ideas?

Thanks

Just to add

I check the levelPosX and y of the player before and after addsprite is called and the values are different

It’d be helpful if you posted some images of the problem and the code you’re using. Some things to look into off the top of my head; in my example above, playerSpawn is a Tiled Object, it is not a tile or a sprite. The x and y parameters of Tiled Objects are in relation to the scale of the map, while the x and y positions of tiles and sprites are in relation to your blockScale. When you check the position of a sprites you should use sprite.getLevelPosX and sprite.getLevelPosY. 

But, like I said, I’d need to see code at least in order to be of any real help.

Well that actually pointed us in the right direction. :slight_smile:

The map was incorrectly made in the Tiled Editor. When the map was made the size of tiles were not correctly made, thus causing an issue with incorrect tile sizes, etc, thus a train wreck of problem finally showed up in the spawning of players and enemies.
Everything works great now. Our little ship moves around and shoots the enemies now.

I will admit, I am the one who screwed up the map. That is what happens when an engineer tries to make his own test art. :wacko: