That's how to control an image with the D-PadZitat:
Zitat von NeodudemanFor control the thing, the code Would be,
The first X=10 and y=10 shows the starting x+y coordinates, which tells us where the image will beCode:x=10
y=10
dude = Image.load("dude.png")
while true do
screen:clear()
pad = Controls.read()
screen:blit(x,y,dude)
if pad:up()
y = y-1
end
if pad:down()
y = y+1
end
if pad:left()
x = x-1
end
if pad:right()
x = x+1
end
if pad:start()
break
end
screen.flip()
screen.waitVblankStart()
end
dude = Image.load("dude.png") loads the image to be used. note that you can change the variable dude to anything you want, but remember to load the right png.
the While true makes it so that all the code within the while and the end are run.
the screen:clear() clears the screen
the pad = Controls.read() gets the input buttons from the PSP. This is needed to use Any of the controls
screen.blit(x,y,dude) displays the dude in all his glory. The blit uses the variable name from before, and not the file name
the If statements say what happens IF certain buttons are pressed.
though you may notice that when you press Up, the Y is subtracted. That goes against all logic! But on a PSP screen, and on many computers, the coordinates are 0,0 at the Top Left corner, which is why you subtract to go up.
the If pad:start() is Definetly useful because it stops the program so you dont have to keep hittin home to quit.
and the screen:flip/Vblank thing display the image
thus concludes simple controlling of an image.
That's controlling with the Analog!Zitat:
Zitat von Neodudemanthe analog stick is a different story Entirely. Its function isnt as linear and simple as the D-Pad's.
the analog stick ranges in intensity from -128 to 128, but can produce values lower than 32 even when it's centered, making it unreliable without the proper code.
Ok. this one Looks tricky, but is actually pretty simple.Code:pad = Controls.read()
ax = pad:analogX()
ay = pad:analogY()
if math.abs(ax) > 32 then
x = x + ax / 50
end
if math.abs(ay) > 32 then
y = y + ay / 50
end
First, the pad = Controls finds the inputs and what not.
Here, ax and ay look for the analog's movement along the X and Y axis, indetified as analogX() and analogY()
Now, the If statement.
If math.abs(ax) > 32 then
remember how i said that the analog can produce values below 32 even when centered? well this statement helps buffer that.
the math.abs(ax) just means the ABSolute value of the ax number. Absolute value changes negative numbers into positive, so that if we get a -32, it'll change it to positive 32. the > 32 makes sure that the absolute value is greater than 32, which is Really what the buffer is.
after all that is tried and set, the x = x + ax / 50 is the adding of that X intensity of the analog to the actual variable x. the /50 is to dull the intensity of the analog. Without it, the x coordinate would be going through some Crazy numbers FAST. So, to lower the addition, we just divide.
See?
Any questions?
Hope that helps!
