Need help with Animated Background Objects

Need help with any SMBX game-related issues? Ask your questions here.

Moderator: Userbase Moderators

Metrovania
Hoopster
Hoopster
Posts: 46
Joined: Mon Nov 04, 2019 11:07 pm
Flair: *obligatory campaign plug*

Need help with Animated Background Objects

Postby Metrovania » Mon Dec 07, 2020 9:40 pm

I'm currently working on a level which used several Animated Background Objects to achieve certain effects. For these effects to work, the animation has to run in it's entirety starting at the first frame. The problem, when the layer the background is on is "Shown", there is no guarantee the background will be on it's first frame. Is there anyway to ensure that a background will be on a certain frame of animation at any given time? Using several layers and quickly switching between them is unfortunately out of the question, since the animation contains a very large number of frames.

I use SMBX2 Beta 4. Any help would be appreciated!

Emral
Cute Yoshi Egg
Cute Yoshi Egg
Posts: 9891
Joined: Mon Jan 20, 2014 12:58 pm
Flair: Phoenix

Re: Need help with Animated Background Objects

Postby Emral » Tue Dec 08, 2020 3:36 am

The frames of all bgos of an Id are synced and not exposed, so it would be safer to try and use an image drawn entirely in lua for this. Here is a small setup that could work for your use case, with some modification of the ALL_CAPS properties at the top:

Code: Select all

local OBJECT_IMAGE = Graphics.loadImage("NAME_OF_THE_OBJECT.png")
local FRAME_COUNT = 20
local FRAME_SPEED = 8 -- frames between frame changes
local OBJECT_X = -200000
local OBJECT_Y = -200300 -- top left corner of object in scene space
local OBJECT_RENDERPRIORITY = -75 -- standard bgo priority

local currentFrame = -1
function onEvent(eventname)
	if eventname == "EVENT_THAT_SHOULD_SHOW_THE_OBJECT" then
		currentFrame = 0
	end
end

-- probably don't need to edit below here
local frameTimer = 0

function onTickEnd()
	if currentFrame >= 0 then
		frameTImer = frameTimer + 1
		if frameTimer == FRAME_SPEED then
			currentFrame = currentFrame + 1
			frameTimer = 0
			if currentFrame > FRAME_COUNT then
				currentFrame = FRAME_COUNT -- CHANGE THIS TO -1 IF YOU WANT IT TO BE HIDDEN AFTER REACHING THE END OF THE ANIMATION
			end
		end
	end
end

function onDraw()
	if currentFrame >= 0 then
		Graphics.drawImageToSceneWP(OBJECT_IMAGE, OBJECT_X, OBJECT_Y, 0, currentFrame * (OBJECT_IMAGE.height / FRAME_COUNT), OBJECT_IMAGE.width, OBJECT_IMAGE.height / FRAME_COUNT, OBJECT_RENDERPRIORITY)
	end
end

This draws an image in onDraw with the frame set based on a timer set in onTickEnd.

Metrovania
Hoopster
Hoopster
Posts: 46
Joined: Mon Nov 04, 2019 11:07 pm
Flair: *obligatory campaign plug*

Re: Need help with Animated Background Objects

Postby Metrovania » Tue Dec 08, 2020 10:04 am

Enjl wrote:
Tue Dec 08, 2020 3:36 am
The frames of all bgos of an Id are synced and not exposed, so it would be safer to try and use an image drawn entirely in lua for this. Here is a small setup that could work for your use case, with some modification of the ALL_CAPS properties at the top:

Code: Select all

local OBJECT_IMAGE = Graphics.loadImage("NAME_OF_THE_OBJECT.png")
local FRAME_COUNT = 20
local FRAME_SPEED = 8 -- frames between frame changes
local OBJECT_X = -200000
local OBJECT_Y = -200300 -- top left corner of object in scene space
local OBJECT_RENDERPRIORITY = -75 -- standard bgo priority

local currentFrame = -1
function onEvent(eventname)
	if eventname == "EVENT_THAT_SHOULD_SHOW_THE_OBJECT" then
		currentFrame = 0
	end
end

-- probably don't need to edit below here
local frameTimer = 0

function onTickEnd()
	if currentFrame >= 0 then
		frameTImer = frameTimer + 1
		if frameTimer == FRAME_SPEED then
			currentFrame = currentFrame + 1
			frameTimer = 0
			if currentFrame > FRAME_COUNT then
				currentFrame = FRAME_COUNT -- CHANGE THIS TO -1 IF YOU WANT IT TO BE HIDDEN AFTER REACHING THE END OF THE ANIMATION
			end
		end
	end
end

function onDraw()
	if currentFrame >= 0 then
		Graphics.drawImageToSceneWP(OBJECT_IMAGE, OBJECT_X, OBJECT_Y, 0, currentFrame * (OBJECT_IMAGE.height / FRAME_COUNT), OBJECT_IMAGE.width, OBJECT_IMAGE.height / FRAME_COUNT, OBJECT_RENDERPRIORITY)
	end
end

This draws an image in onDraw with the frame set based on a timer set in onTickEnd.
I'm sorry but I'm not very familiar with LUA script, where exactly would I use this? In a .lua file?

Added in 19 minutes 38 seconds:
So far it's not working, though I could be implementing the script wrong. I've changed a couple things to fit in the level, here is the edited script:

local OBJECT_IMAGE = Graphics.loadImage("background-123.png")
local FRAME_COUNT = 10
local FRAME_SPEED = 6
local OBJECT_X = -79552
local OBJECT_Y = -80352
local OBJECT_RENDERPRIORITY = 100

local currentFrame = -1
function onEvent(topplestart)
if eventname == "topplestart" then
currentFrame = 0
end
end

-- probably don't need to edit below here
local frameTimer = 0

function onTickEnd()
if currentFrame >= 0 then
frameTimer = frameTimer + 1
if frameTimer == FRAME_SPEED then
currentFrame = currentFrame + 1
frameTimer = 0
if currentFrame > FRAME_COUNT then
currentFrame = FRAME_COUNT -- CHANGE THIS TO -1 IF YOU WANT IT TO BE HIDDEN AFTER REACHING THE END OF THE ANIMATION
end
end
end
end

function onDraw()
if currentFrame >= 0 then
Graphics.drawImageToSceneWP(OBJECT_IMAGE, OBJECT_X, OBJECT_Y, 0, currentFrame * (OBJECT_IMAGE.height / FRAME_COUNT), OBJECT_IMAGE.width, OBJECT_IMAGE.height / FRAME_COUNT, OBJECT_RENDERPRIORITY)
end
end

Ideally, this would start the BGOs animation when an event called "topplestart" is run. I set the BGOs X/Y coordinates to what they are ingame, and I think there was a typo in the line "frameTimer = frameTimer + 1" (it said frameTImer the first time). Also, like I asked earlier, where exactly do I put this script in regards to my level? In a simple .lua file, or the "your code here" in the template leval luna file?

Emral
Cute Yoshi Egg
Cute Yoshi Egg
Posts: 9891
Joined: Mon Jan 20, 2014 12:58 pm
Flair: Phoenix

Re: Need help with Animated Background Objects

Postby Emral » Wed Dec 09, 2020 2:57 am

Metrovania wrote:
Tue Dec 08, 2020 10:24 am

Ideally, this would start the BGOs animation when an event called "topplestart" is run. I set the BGOs X/Y coordinates to what they are ingame, and I think there was a typo in the line "frameTimer = frameTimer + 1" (it said frameTImer the first time). Also, like I asked earlier, where exactly do I put this script in regards to my level? In a simple .lua file, or the "your code here" in the template leval luna file?
This should replace the contents of the template luna.lua.
Mind you, Render Priority 100 is above everything.
If that doesn't work, you'll have to be more specific about what exactly is the problem. Are there any warnings or error popups? What do you see and what do you expect to see?

Metrovania
Hoopster
Hoopster
Posts: 46
Joined: Mon Nov 04, 2019 11:07 pm
Flair: *obligatory campaign plug*

Re: Need help with Animated Background Objects

Postby Metrovania » Wed Dec 09, 2020 9:53 am

So, I've decided that I must be in way over my head. Please understand I have NO experience with Lua, so sorry if I came across as a little ignorant. I've also decided it'd just be easier (though tedious) to simply have a unique layer for each frame of animation. Thanks for the help anyways! Maybe in the future I'll revisit this "style" of in-game animation, but for now I'm just gonna do it old-school.


Return to “Help and Support”

Who is online

Users browsing this forum: No registered users and 0 guests

SMWCentralTalkhausMario Fan Games GalaxyKafukaMarioWikiSMBXEquipoEstelari