Hey everyone. In my computer science class we’re suppose to write code that displays a sprite and the user can drag it with their finger. We’re suppose to follow our teacher’s tutorial but for some reason it doesn’t work for me. The code is the same as his. Any help would be appreciated
-- This program displays a sprite and the user can move it with their finger
-- global variable
imageName = "SpaceCute:Beetle Ship"
imageSize = vec2(spriteSize(imageName))
imagePosition = vec2()
-- Use this function to perform your initial setup
function setup()
supportedOrientations(LANDSCAPE_ANY)
displayMode(FULLSCREEN)
noFill()
noSmooth()
noStroke()
pushStyle()
imagePosition = vec2(WIDTH/2, HEIGHT/2)
end
-- This function gets called once every frame
function touched(touch)
-- local variables
local currentTouchPosition = vec2(touch.x,touch.y)
if(touch.state == BEGAN)then
end
if(touch.state == MOVING)then
if( (imagePosition.x - imageSize.x/2) < currentTouchPosition.x and
(imagePosition.x + imageSize.x/2) > currentTouchPosition.x and
(imagePosition.y - imageSize.y/2) < currentTouchPosition.y and
(imagePosition.y + imageSize.y/2) > currentTouchPosition.y )then
imagePosition = currentTouchPosition
end
end
if(touch.state == ENDED)then
end
end
-- This function gets called once every frame
function draw()
-- local variables
-- This sets a dark background color
sprite("SpaceCute:Background", 512, 384, 1024, 768)
sprite(imageName, imagePosition.x, imagePosition.y)
end
@Zacharias looks like you found a bug in Codea. Thank you for posting your code!
The spriteSize function does not work until setup() has been run. So if you modify your code to declare your global variables in setup it should work:
-- Use this function to perform your initial setup
function setup()
-- Global variables
imageName = "SpaceCute:Beetle Ship"
imageSize = vec2(spriteSize(imageName))
imagePosition = vec2()
supportedOrientations(LANDSCAPE_ANY)
displayMode(FULLSCREEN)
noFill()
noSmooth()
noStroke()
pushStyle()
imagePosition = vec2(WIDTH/2, HEIGHT/2)
end
I’ll look into why it’s not returning correct values prior to setup().
@Ignatz your suggestion works as well. Although my class has only just begun learning the language so I don’t wanna confuse my classmates with code we haven’t learned about yet. Still thanks a lot!
@Ignatz - Yeah I’m sorry I just figured posting my exact code and just not looking through the forum would be a quicker solution. If I have any other questions I’ll look through the forum first. :-S
@Zacharias There’s 4 years of information on the forum. The coding and information range from basic to complex. I’ve been here for about 3 1/2 years and I’m still learning new things searching thru the forum.