The learning curve is a bit steep because there aren't commands for everything. The computer thinks a bit differently than you or I, so it's important to translate your ideas into something this hunk of metal understands.
With regards to the above snippit: It loads the image at the top of the file. The top of the file is executed whenever the level loads, so you can be sure to have the image available in the variable named "img", as you correctly noted. Worth knowing is that calling the variable something else like "pictureOfMario" is just as valid! Variable names are there for you, the human, to make sense of the use you have for what is stored within.
Next, the "function onDraw" is a lunalua event (built-in function) which executes once every frame in-game, around the time when SMBX draws its gameplay elements to the screen. There are a lot more lunalua events (
https://docs.codehaus.moe/#/reference/lunalua-events), but for now the important thing to note is: Graphics.drawImage, in the above code, doesn't permanently place an image on the screen, but instead renders it anew every frame.
So with regards to "erasing" it, what you need to tell the computer is "When should I draw it?" - this is what the common programming tool, the if statement, is for. Here's a slightly altered version of my example.
Code: Select all
local img = Graphics.loadImage("fileName.png")
local shouldDrawTheImage = false
function onDraw()
if player.keys.down == KEYS_PRESSED then
shouldDrawTheImage = not shouldDrawTheImage
end
if shouldDrawTheImage then
Graphics.drawImage(img, 0, 0)
end
end
This augmented example introduces a second variable: shouldDrawTheImage. It's a boolean (can be either false, or true. Think of it as flipping a light switch.). The Graphics.drawImage is now encapsulated (programming word meaning "within") an if statement which only runs its contents if that variable is true, not false. Directly above, another if statement is placed. This one toggles the variable. The if statement here is a bit much to take in, since it uses a lot of SMBX2 native jargon, but in essence: "player" is a pre-defined variable for the first player, keys.down is where you can read the current frame's value of the down directional button, and the comparison == KEYS_PRESSED checks if a tap on the button occurred.
To rephrase that: When player 1 presses down, the light switch's state is changed! On frames where it's ON (true) the image draws, otherwise not.
The big takeaway here is that the condition that causes the boolean to be toggled can be virtually anything. Using the onEvent lunalua event, you can make the image draw as soon as a SMBX event is triggered, for instance.
Code: Select all
local img = Graphics.loadImage("fileName.png")
local shouldDrawTheImage = false
function onDraw()
if shouldDrawTheImage then
Graphics.drawImage(img, 0, 0)
end
end
function onEvent(eventName)
if eventName == "P Switch - Start" then
shouldDrawTheImage = not shouldDrawTheImage
end
end
Now it shows the image when a P-Switch is pressed (and hides it when the next p-switch is pressed).
One last note: Since it all happens anew every frame, MOVING images follows the same principle: Using a variable whose state changes every frame, we can exchange the 0, 0 for the images X and Y coordinates with dynamic values. Here's a quick and dirty sine wave motion along the X axis:
Code: Select all
local img = Graphics.loadImage("fileName.png")
local timer = 0
function onDraw()
timer = timer + 1
local sineWavePosition = math.sin(timer/100) * 64
Graphics.drawImage(img, sineWavePosition, 0)
end