How do I resize or scale a sprite when touched and also make it move moveable while touching it.

Hi, I’m only 3 weeks into coding so this might be a noob question. Anyways I want to scale a sprite, that’s already on screen, when I touch it. I want that same sprite to move around while I keep my finger on it.

Let me know if I’m going about this the wrong way.

Here’s the sample project below

--Sample Project

imagePosition = vec2()
imageName = asset.builtin.SpaceCute.Beetle_Ship
imageSize = vec2(spriteSize(imageName))

function setup()
    displayMode(FULLSCREEN)
    imagePosition = vec2(WIDTH/2, HEIGHT/2)
    imageName = asset.builtin.SpaceCute.Beetle_Ship
end

function draw()
    background(0, 191)
    sprite(imageName,WIDTH/2,HEIGHT/2)
if CurrentTouch.state==BEGAN then
        pushMatrix()
        translate(CurrentTouch.x,CurrentTouch.y)
        scale(1.45)
        sprite(imageName, 0, 0)
        pushMatrix()
end

if CurrentTouch.state==MOVING then
        pushMatrix()
        translate(CurrentTouch.x,CurrentTouch.y)
        scale(1.45)
        sprite(imageName, 0, 0)
        pushMatrix()
end
end

@Jarc - firstly please enclose your code with ~~~ before and after to format it properly for the forum.

Now try this:


displayMode(FULLSCREEN)
function setup()
    —
    imageName = asset.builtin.SpaceCute.Beetle_Ship
    imageSize = spriteSize(imageName)
    imagePosition = vec2(WIDTH/2, HEIGHT/2)
    imageName = asset.builtin.SpaceCute.Beetle_Ship
    scaler = 1
end
    
function draw()
   —
    background(0,0,0,255)
    sprite(imageName,WIDTH/2,HEIGHT/2,imageSize*scaler)
end

function touched(touch)
    --
    if touch.state==BEGAN or touch.state == MOVING then
        scaler = 2
    else
        scaler = 1
    end
end

Hope that helps - many ways of doing this

Edit: Moved displayMode outside setup() and - thanks for reformatting.

@Jarc Here’s an example to move and scale the sprite. Touch the screen once and slide your finger to move the sprite. Touch twice and slide your finger up or down to scale the sprite.

PS. I added the ~~ to your code above.

displayMode(FULLSCREEN)

function setup()
    imageName = asset.builtin.SpaceCute.Beetle_Ship
    imageSize = spriteSize(imageName)
    imagePosition = vec2(WIDTH/2, HEIGHT/2)
    imageName = asset.builtin.SpaceCute.Beetle_Ship
    scaler = 1
    x,y=WIDTH/2,HEIGHT/2
    ss=20
end

function draw()
    background(0, 191)
    sprite(imageName,x,y,scaler*imageSize)
end

function touched(touch)
    if touch.state==MOVING then
        if touch.tapCount==1 then
            x=x+touch.deltaX
            y=y+touch.deltaY
        end
        if touch.tapCount==2 then
            ss=ss+touch.deltaY
            if ss<0 then
                ss=0
            end
            scaler=ss/20
            if scaler<.1 then
                scaler=.1
            end
        end
    end
end

@dave1707 &
@Bri_G - First off, thank you both for answering so promptly.

Is there a way to do this in one touch & slide?

Meaning touch the sprite on screen once, it scales and sliding my finger around will move the scaled sprite.

Let me know if that description is confusing.

@dave1707 & @Bri_G
I actually figured it out. Thank you both very much! I couldn’t have done it without both of you.


displayMode(FULLSCREEN)

function setup()
    parameter.watch("scaler")
    parameter.watch("ss")
    imageName = asset.builtin.SpaceCute.Beetle_Ship
    imageSize = spriteSize(imageName)
    imagePosition = vec2(WIDTH/2, HEIGHT/2)
    imageName = asset.builtin.SpaceCute.Beetle_Ship
    scaler = 1
    x,y=WIDTH/2,HEIGHT/2
    ss=20
end

function draw()
    background(0, 191)
    sprite(imageName,x,y,scaler*imageSize)
end

function touched(touch)
    if touch.state==BEGAN then
        scaler = 2
    else
        scaler = 1
    end

    if touch.state==MOVING then
        if touch.tapCount==1 then
            x=x+touch.deltaX
            y=y+touch.deltaY

        scaler = 2
    else
        scaler = 1
    end

    end
end

@Jarc I just assumed you wanted to scale the sprite more than twice it’s size. You also say you want to scale and move it if you touch the sprite. Here’s some code that will size and move the sprite only if you touch it near the middle.

displayMode(FULLSCREEN)

function setup()
    imageName = asset.builtin.SpaceCute.Beetle_Ship
    imageSize = spriteSize(imageName)
    imagePosition = vec2(WIDTH/2, HEIGHT/2)
    imageName = asset.builtin.SpaceCute.Beetle_Ship
    scaler = 1
    x,y=WIDTH/2,HEIGHT/2
end

function draw()
    background(0, 191)
    sprite(imageName,x,y,scaler*imageSize)
end

function touched(touch)
    if touch.state==BEGAN then
        if touch.x>x-80 and touch.x<x+80 and touch.y>y-80 and touch.y<y+80 then
            scaler = 2
        end
    end
    if touch.state==MOVING and scaler==2 then
        x=x+touch.deltaX
        y=y+touch.deltaY
    end
    if touch.state==ENDED then
        scaler=1
    end
end

@dave1707 That’s exactly what I wanted. Wow spot-on, thanks again!