Are borders even possible here?

Hello, new to the forums, coming from c#. Currently I am working on the basics of sprite control. I have implemented a joystick that generates wherever u press ( I would like a stationary one, but I’ll work on that later). So basically, I can control the sprite, but I’ve searched for ways to keep it inside the screen but it still floats freely off the sides and out of site. I am curious if the joystick code would allow the sprite to go outside the border even if there were some in place…

function setup()
    x,y=0,0
    sx=WIDTH/2
    sy=HEIGHT/2
end

function draw()
    background(40, 40, 50)
    fill(255)
    
    
    sprite("Planet Cute:Character Boy",sx,sy)
    if x+y>0 then
        sx=sx+(tx-x)/10
        sy=sy+(ty-y)/10
        fill(255)
        ellipse(x,y,10)    -- draw circle center
        noFill()
        stroke(255)
        strokeWidth(4)
        ellipse(x,y,100)    -- draw outer circle
    end
end

    
    


function touched(t)
    if t.state==BEGAN then    -- starting center point
        x=t.x
        y=t.y
        tx=x
        ty=y
    elseif t.state==MOVING then
        tx=t.x
        ty=t.y
    elseif  t.state==ENDED then    -- done moving
        x,y=0,0
        
        
        end
            
   end

Above is the code I currently have, if this were C# I would just use an if statement:

If (sprite.X <= 0)
   Then sprite.X = 0)

I am unsure of the syntax with Lua and have searched many if the. Statements in Lua but none of them work and I’m starting to think its because of the joystick method. If anyone has any advice, I’d greatly appreciate it. Thank you.

Thank you @Ignatz , i don’t see a lot of difference between that and mr ninjas, but I’m sure it will make a difference as more code is entered and it’s not constantly drawing. I also noticed the transparent pixels, but I’m just using the image as a template so there’s no problem there. Thank you all again for ur help. Have fun.

@Theonegoku - Try this code, see changes marked 1 and 2, and notes underneath

function setup()
    x,y=0,0
    sx=WIDTH/2
    sy=HEIGHT/2
    img=readImage("Planet Cute:Character Boy")  --[1]
    w,h=img.width/2,img.height/2
end

function draw()
    background(40, 40, 50)
    fill(255)
    sprite(img,sx,sy)
    if x+y>0 then
        sx=sx+(tx-x)/10
        sy=sy+(ty-y)/10
        if sx > WIDTH-w then sx= WIDTH-w  elseif sx<w then sx = w end --[2]
        if sy > HEIGHT-h then sy = HEIGHT-h elseif sy < h then sy = h end
        fill(255)
        ellipse(x,y,10)    -- draw circle center
        noFill()
        stroke(255)
        strokeWidth(4)
        ellipse(x,y,100)    -- draw outer circle
    end
end

function touched(t)
    if t.state==BEGAN then    -- starting center point
        x=t.x
        y=t.y
        tx=x
        ty=y
    elseif t.state==MOVING then
        tx=t.x
        ty=t.y
    elseif  t.state==ENDED then    -- done moving
        x,y=0,0
    end
end

[1] - it’s better to only load the image once into memory, than 60 times per second in draw

[2] - I calculate half the width and height of the image in setup and put them in w,h . Then in this line and the next, I use them to check that the position doesn’t go beyond the edge of the screen. The reason I use half width and height is that the image is centred on its x,y position, so the distance from sx,sy to the edge of the image is only half the width and height.

(If you’re wondering why the little dude doesn’t go all the way to the edge of the screen, it’s because the image has transparent pixels surrounding it).

Thanks @theonegoku. I’m not quite sure what the problem is, but it stutters when i push it near the edges, ill post if i find a solution, and good luck on your apps!

EDIT: place that exact same code in draw, right before you call the sprite, and it removes that stuttering problem

@Theonegoku - by the way, to format code correctly, put three ~ on a line before, and after it. I fixed your code above.

Ok a couple things, thank you @ignatz, I was curious about the formatting, I’ll do that from now on, secondly, really @Mr_Ninja ?, are you out of your mind? Your only 13? Lol, that’s crazy man, ur editing of the sprite size and if statement worked just like I wanted it to, very good work mate. The top left and bottom and stop him in his tracks, he still goes through the right wall but I’m sure it’s just a missed comma or something so I’ll troubleshoot it, thank you again, this is one less thing I have to worry about while I figure out the stationary joystick setup. Have fun.

EDIT: ok Mr_Ninja no biggie! ur auto capitalize failed u, you had Sx instead of sx, I updated it and it’s perfect now, thank you again.

And here it is with spriteSize, add it to your app in MOVING and it works, tested.

w , h = spriteSize("Planet Cute:Character Boy")

 if sx> WIDTH - w/4 then
    Sx = WIDTH -w/4
    end
    if sy > HEIGHT -h/4 then
        sy = HEIGHT -h/4
    end
    if sx < w/4 then
        sx = w/4
    end
    if sy < h/4 then 
        sy = h/4
    end

This code should work with your app (hopefully)

Hope this helps, not quite sure if this is what you are looking for though.



 if self.position.x > WIDTH then
    self.pos = WIDTH -50
    end
    if self.position.y > HEIGHT -50 then
        self.position.y = HEIGHT -50
    end
    if self.position.x < 50 then
        self.position.x = 50
    end
    if self.position.y < 50 then 
        self.position.y = 50
    end

This is assuming you have

self.position = vec2 (_____,_____)--set starting position in blanks

And your joystick edits those position.

Edit: I chose the number 50 because it seemed to leave no space with a circle thats 200x200, I’m sure there’s a way to use spriteSize to get the proper amount if pixels for the sprite that your using, but don’t quite know how. Heres the code that would work with your app.

 if sx > WIDTH -50 then
    sx = WIDTH -50
    end
    if sy > HEIGHT -50 then
        sy = HEIGHT -50
    end
    if sx < 50 then
        sx = 50
    end
    if sy < 50 then 
        sy = 50
    end

PS, I’m 13 years old and beginner programmer, with only about a month of experience, so im sure there are way more efficient ways of doing this.

@Theonegoku - Codea draws completely differently from C.

My post here explains.

I think you can replace some of the if statements with math.min and max like so:

sy=math.min(sy,HEIGHT-h)
sy=math.max(sy,0+h)
sx=math.min(sx,WIDTH-w)
sx=math.max(sx,0+w)

@Coder - why stop there? :smiley:

sy=math.max(math.min(sy,HEIGHT-h),0+h)
sx=math.max(math.min(sx,WIDTH-w),0+w)

lol. Code readibility is important too.

@Jmv38 - ok, I’ll try again :wink:

sy,sx=math.max(math.min(sy,HEIGHT-h),0+h),math.max(math.min(sx,WIDTH-w),0+w)

yeah, that’s much better now! :-w