Dusk Engine

About (smoothly) scrolling background :wink:

I’ve set the camera focus to the player and tracking level is about 0.1. I try to add a scrolling background image that should move with the player, but not as fast. The player moves like 3 pixels and the background 1 pixels in a frame. But because the camera movement is smooth, it looks pretty weird when the player goes right and stops, the scrolling background also stops, but the camera still moves a bit (because of the necessary smoothess). Pretty hard to explain :wink:

The point is - How could I also move the background image smoothly (and not like background.x=background.x-1)? For example, could I access the movement of the camera of the Dusk?

This is not a pure Dusk question, but I think that all of us who are working with tiles, need this :slight_smile:

Hi Caleb!

Question 1:

I got the same problem as Painconfess. If I’ve created enemy with the build code above. How do I remove the object, if it is destroyed? And when the host object is created again after beeing culled, how do I prevent that the object is not created again?

Question 2:

How can I call the function: objectType.remove = function(object)

Question 3:

If I create a new object like mySpecialObjectType2. How do I do that? You said:“The easiest way to make object types is with separate modules. It even gives you a sort of object-oriented feel. This code only needs to be called once. It’s like a “hey, Dusk, do this and this when you draw and erase an object” function.

Do you mean something like this: https://coronalabs.com/blog/2012/08/28/how-external-modules-work-in-corona/

You said "_This code only needs to be called once" _ - do you mean once per object type? 'Cause the way I understood the code, is that the code is diffenent if the object is different. For example “local object = display.newRect(0, 0, event.object.width, event.object.height)”   could be “local object = display.newImageRect(“image.png”, 40, 40)” and other properies are also different (for example hit point).

Is it possible to have a example code where you have two object types in modules and maybe with this remove thing (question 1) :slight_smile:

Aatos Media,

I haven’t tried this but have you tried to set the host’s Type to different value after the child is built to prevent it’s rebuild? This might again be a bad way to do this if this works but I had to share my idea :smiley: If you want to host appear after culling if it’s not destroyed by player then I don’t know what to do. Some how you need to iterate through child to host to set the Type…

And I think the removeSelf() or display.remove(object) is right way to do because in objectType.remove function uses the same method.

I have at the moment 2 different enemies in my map and both have own objectTypes in my gamedata.lua and have the addObjectType function in game.lua.

gamedata.lua:

[lua]

local F = {}

function F.EnemyList()

        local objectType = {}

        objectType.objectType = “Enemy”

        – A function which creates your custom object

        objectType.build = function(event)

          local object = display.newSprite( sheetPic, sheetSequenceData)

          physics.addBody( object, “static”, {radius = 25} )

          object.x, object.y = event.object.x, event.object.y

          event.object.parent:insert(object)        

          return object

        end

        – A function which deletes your custom object

        objectType.remove = function(object)

          display.remove(object)

        end

 return objectType

end

function F.BossList()

        local objectType = {}

        objectType.objectType = “Boss”

        – A function which creates your custom object

        objectType.build = function(event)

          local object = display.newSprite( sheetPic, sheetSequenceData)

          physics.addBody( object, “static”, {radius = 83})

          object:setSequence( “spin” )

          object:play()

          object.x, object.y = event.object.x, event.object.y

          event.object.parent:insert(object)

              end

          end

          return object

        end

        – A function which deletes your custom object

        objectType.remove = function(object)

          display.remove(object)

        end

 return objectType

end

return F

[/lua]

game.lua:

[lua]

local gamedata = require( “gamedata” )

local BL = gamedata.BossList()

local EL = gamedata.EnemyList()

  addObjectType(EL)

  addObjectType(BL)

[/lua]

I stripped some unnecessary lines like variables for sprites etc.

I hope this helps!

@Aatos Media:

You can insert the scrolling background into an empty layer of the map, then set the layer’s xParallax/yParallax as needed. That way, it’ll move exactly in the same way as the rest of the map.

Q1: Just make sure your remove() function doesn’t fail if the object is already removed, then call it to remove the object when you want to. Then, when the host object is culled, the remove function will be called but nothing will happen. The addObjectType code adds a listener for the type, so you could technically set the type of the object to something else (I think?), but a much easier and safer way is to just set a flag for something specific about the host object, like position or name. Here’s an example for position, built on the idea that no two host objects in a layer will have the same position and object type.

local dontComeBackOrIllShoot = {} -- Table to hold references to destroyed objects -- Yackety yack -- Inside an object's destroy function (when your bullet hits it or whatever) local specialKey = object.x .. "|" .. object.y -- Make a magical key to identify the host object; it's important to note that 'object' here refers to the host object, not the child object dontComeBackOrIllShoot[specialKey] = true -- Set the flag in the destroyed objects table -- Yackety yack -- Inside the objects build function local specialKey = event.object.x .. "|" .. event.object.y if dontComeBackOrIllShoot[specialKey] then return -- Never mind, this one's already been destroyed end

Q2: I don’t really understand this question :(. Can you explain some more please?

Q3: The way Painconfess did it works. You can put the object types into separate modules, then require() them and add the object types to the map. Alternatively, you can do it how I do it and make a separate module for each object type:

-- types/movingPlatform.lua local objectType = {} objectType.build = ... objectType.remove = ... return objectType

-- types/floorSwitch local objectType = {} objectType.build = ... objectType.remove = ... return objectType

Then, in your game file:

addObjectType(require("types.movingPlatform")) addObjectType(require("types.floorSwitch"))

And yes, I did use “called once” to mean “called once per object type”.

  • Caleb

It seems like when you use map.destroy() it doesn’t call the objectType.remove to the added & displayed objects. I have moving enemies and shooting enemies now and 1st I had errors when culling removed the objects, but got that fixed by adding the lines transition.cancel(object) and timer.cancel(object.shooting) in objectType.remove function.

But now if enemy is displayed and scene is changed (to gameover for example) I get the same error (nil value from object.x or something). So I managed with the moving enemies to set in scene:destroy the line: transition.cancel(), but the timer.cancel(object.shooting) is a bit trickier. I tried using for loop where iterate all whose typeIs(“shootingEnemy”) and timer.cancel() each, but noticed it only iterates host objects.

So in short I am asking how do I iterate child objects? :smiley: Or how could I make the displaying enemies do their own remove function before I change the scene…?

Question 2 in long version. If I have added object culling like in Painconfess’s example (one gamedata.lua file) and I have collision handler function in main.lua. Now - how can I code that I want to call remove function from the module gamedata.lua and from the Enemylist function? Sorry - this is complicated, but I think the structure is a bit complicated too :wink: Or If you prefer your example (one module per type), how could I call remove function from the main.lua when the remove function is in types/movingPlatform.lua

I thought it would be something like (painconffess case):

module (gamedata) --> function (EnemyList) – > function (objectType.remove) --> pass removable object (event.object2)

If this is possible, I think I’m heading to a new problem. If the collision happens, how I know if the object is Enemy of Boss (I think this could be managed, but the logic is becoming too complicated). Or should I put the collision detection into to build-function of the objectType.build = function(event)?

Sorry about the confusing message - but I’m pretty confused :slight_smile:

For collision detection set for example object.myName = “enemy” in objectType.build and if you want to detect collision with player or bullet. Make collision eventlistener for player/bullet and check if event.other.myName == “enemy”.

I think I’ve done exactly this, but where did you place the collision event listener? 'Cause mine was in main.lua, but now I have problems to call the remove-function.

I don’t know does it matter is it in main.lua, but all the player’s data I have in gamedata.lua also… you use remove function as [lua]event.other:removeSelf() event.other = nil [/lua] ? If so then what is the error?

I feel stupid. I somehow understood that you have to use the remove function of the object culling code :slight_smile:

Thanks!

No problem :slight_smile:

I assume this was the question 2 you needed to explain more to Caleb? I was thinking how to use that function but I found it difficult because it is objectType’s function, not the not the objectType.build function’s object (the child object’s) function .

Mabye now Caleb knows how to reply to this issue :slight_smile:

Question 2 is done now :wink:

But @Caleb - I’ll get an error here
 

if dontComeBackOrIllShoot[specialKey] then   return -- Never mind, this one's already been destroyed end

main.lua:71: attempt to index local 'obj' (a nil value) stack traceback: main.lua:71: in function '?' Dusk\dusk\_core\layer\objectlayer.lua:232: in function 'constructObject' Dusk\dusk\_core\layer\objectlayer.lua:432: in function 'draw' Dusk\dusk\_core\misc\editqueue.lua:53: in function 'execute' Dusk\dusk\_core\run\culling.lua:142: in function 'update' Dusk\dusk\_core\run\update.lua:126: in function 'updateView' main.lua: 900 in function \<main.lua: 852\> ?: in function \<?:221\>

in main.lua

line 71: obj.mapObject = event.object    (this is the function: addObjectType(params))

line 900: map.updateView()

I wrote the error message - :lol:

EDIT

Don’t know if this a good way, but at least it works:

objectType.build = function(event) &nbsp; local objectid = event.object.x .. "|" .. event.object.y &nbsp; local object = display.newImageRect("image.png", 40, 40) &nbsp; if globals.hitPoints[objectid] == 0 then &nbsp;&nbsp;&nbsp;&nbsp; return object &nbsp; end

@Aatos Media:

Good catch. Didn’t even think about that. You can add a nil check in the addObjectType function for that.

You probably don’t want to use up the resources to create an object if the object doesn’t need creating, which is why I was just exiting the function if it was destroyed. So a nil check is probably a better idea than creating the object whether it should be or not. That said, if you don’t mind the wasted memory, that’ll work, too.

@Painconfess: I’ve lost the thread of this latest conversation, sorry; do you still have an open question?

  • Caleb

Yes, I am wondering how can I iterate through child objects? I am having errors with displayed objects when I am changing scene, because some objects has transition’s and timer’s functions which needs to be cancelled before leaving current scene. Is there a easy way to call every displaying object’s remove function when leaving the scene and using map.destroy()?

I’m back! Post away!

  • Caleb

Hi Caleb,

I have been having some issues with loading tile maps into my scene.

I build the map in my code and it launches with no errors, but the tiles are all mixed up and there are large gaps in between tiles.

I was thinking this may have something to do with the spacing on the tileset. Does the Dusk Engine have any limitations concerning margins and tile spacing?

Thankyou in advance.

There are no issues with margin or spacing as far as I know. Can you give me some more information about your map? What’s your tile size, margin, spacing, and config.lua settings? Does this issue occur on every simulator, or just one?

  • Caleb

I just pushed an update to the dev branch that calls the erase listener when each object layer is destroyed. So that should do it for you. Now when you destroy() a map, each currently drawn object calls its erase listener, which is what we hook into in addObjectType.

Also, I’m going to be gone for the next week, so I won’t be able to answer questions.

  • Caleb

Just pushed a cool new “dev tag” (my new name for pre-1.0 releases) to GitHub. Read the tag information at https://github.com/GymbylCoding/Dusk-Engine/blob/master/LATEST_TAG_NOTES.md for full information.

Anyhow, the coolest thing in the update (IMO) is the new edge mode feature. You can set a tile layer’s “edgeModeX” or “edgeModeY” properties to control how the tile layer’s culling/drawing is done. To make tiles in a layer wrap around in the X-axis when the culling reaches the edge, you set “edgeModeX” to “wrap”. A value of “clamp” will clamp to the farthest tile in either dimension, and “stop” (the default) will stop drawing when it reaches the edge.

  • Caleb

Thanks!

I don’t know why but [lua]dusk.setPreference(“virtualObjectsVisible”, false)[/lua] doesn’t seem to work with the new Dusk… Or the reason is somewhere else.