Okay so here goes nothing… this is a slight learning curve from all the other languages i already know…
Just a simple question, wanted to start simple…
if i have a background1 sprite image and background sprite background2 image and also two scripts with the same names how do i get the scripts to execute upon running?
All files in the background collection
Does the script know it is using self as the background1 image since the script is named background1 also just trying to get my head around how the script ties in with the images, etc…
Just need someone to point out the obvious i guess…
just the simple script taken from online manual
function init(self)
msg.post("@render:", “clear_color”, { color = vmath.vector4(0.52, 0.80, 1, 0) } )
end
– the background is a tilemap in a gameobject
– we move the gameobject for the parallax effect
function update(self, dt)
– decrease x-position by 1 units per frame for parallax effect
local p = go.get_position()
p.x = p.x + 1
go.set_position§
end
You can use the same script for both backgrounds and set up a property that takes a number of pixels the background should move each second (or frame if you want it that way).
go.property("pixels_per_second", 1)
function init(self)
self.position = go.get_position()
end
function update(self, dt)
self.position.x = self.position.x - dt * self.pixels_per_second
go.set_position(self.position)
end
I understand that bit but how to you get some code to run without pressing a button it just runs all the time as in a loading screen that will disapear after so many seconds but rotates, scales etc before being removed from display
Any game object directly in the main collection or in a collection that in turn has been put in main collection will load, init and run on game start.
If you put components in such a game object the component will initiate and “run” as well. For a sprite, that means display and for a script component that means the script executes. So you just add script components to your game objects and point them to the script you would like to execute. The init() function is run when the object is created and update() is ran once every frame for the lifetime of the object.
As for “self”. That is a reference to the game object itself. You can use it to store data, like “self.my_data = 42”.
If you have several script components in a game object, they will all share the same “self”. If you have two game objects with script components that share the same script file, they will not share the same “self” since they live in different game objects.
Thanks Sicher, all im trying to do is rotate a sprite on upon loading but cant even manage to get this basic thing working.
Only been learning this system for a few hours kinda breaking away from the tutorials a bit as i feel this is how i learn better.
I understand the self part as its the same in C++ with regards to referencing to objects so running a script within a collection within an object will reference the sprite held within the object, just stuck on which function to rotate sprite without any actions such as a key press.
@AppsTrader: You need to understand a couple of concepts to achieve what you describe:
Collections A collection of game objects. You only need one collection, but for a larger game it might make sense to have many. Your app will start with the collection specified in game.project (bootstrap.main_collection). From this collection you can load more collections
Game objects. Things in your game world. Every game object has a position, scale and rotation. A game object consists of components.
Components. Attached to game objects. Sound, Sprites, Scripts etc
Scripts. A script added to a game object in a loaded collection will have it’s lifecycle functions called when appropriate: init() called when script is loaded, update() called every frame, on_input() called when input is received and so on
So, in order for you to get a sprite to rotate automatically you need a collection with a game object. The game object should have a sprite and a script component. The script can either rotate the game object using go.set_rotation() every frame in the update() function or by animating the rotation using go.animate() with a looping animation in the init() function of the script.