I need help removing sprites
How can I modify this code so that the sprites are deleted after a second touch and then new random sprites are drawn?
This is the current code:
draw = function()
for t in touch.touches
screen.drawSprite(randomSprite(),0,0,70,70)
screen.drawSprite(randomSprite(),120,0,60,60)
screen.drawSprite(randomSprite(),-120,0,70,70)
end
end
you can use this: " ``` " for code display
draw = function()
for t in touch.touches
screen.drawSprite(randomSprite(),0,0,70,70)
screen.drawSprite(randomSprite(),120,0,60,60)
screen.drawSprite(randomSprite(),-120,0,70,70)
end
end
as far I know, you'd have to create a variable for each of them and only draw the sprite if it's less than two touches
here's an example
init = function()
hit1 = 0
hit2 = 0
hit3 = 0
end
update = function()
collision detection here
end
draw = function()
for t in touch.touches
if hit1 < 2 then
screen.drawSprite(randomSprite(),0,0,70,70)
end
if hit2 < 2 then
screen.drawSprite(randomSprite(),120,0,60,60)
end
if hit3 < 2 then
screen.drawSprite(randomSprite(),-120,0,70,70)
end
end
end
you could also make them part of group so you don't need an if statement for each
// Actor class that takes x, y, width, height and a sprite image as parameters
Actor = class
constructor = function(x, y, width, height, sprite)
this.x = x
this.y = y
this.width = width
this.height = height
this.sprite = sprite
// Hit counter to track the number of times the actor has been hit
this.hitCount = 0
end
// Method to check if a given point (px, py) lies inside the actor's boundaries
containsPoint = function(px, py)
// Return true if the point is within the rectangular area defined by the actor
if ( px >= this.x - this.width / 2 and px <= this.x + this.width / 2 and
py >= this.y - this.height / 2 and py <= this.y + this.height / 2 ) then
return true
else
return false
end
end
draw = function()
screen.drawSprite( this.sprite, this.x, this.y, this.width, this.height )
end
end
// Collection class that manages a list of Actor objects
ActorsCollection = class
constructor = function()
this.actors = []
// Create 10 Actor objects with random positions
for i = 0 to 10 by 1
local x = floor(random.nextInt( screen.width ) - screen.width / 2 )
local y = floor(random.nextInt( screen.height ) - screen.height / 2)
// Create an Actor with fixed width and height, and a new Image as sprite
actor = new Actor(x, y, 50, 50, sprites["icon"] )
this.actors.push(actor)
end
end
// Method that checks if a given point (px, py) is inside any actor
// If an actor is hit, increase its hit counter; when hit twice, remove the actor
// and add a new Actor at a random position
checkPoint = function(px, py)
// Loop backwards over the array to safely remove elements
for i = this.actors.length - 1 to 0 by -1
local actor = this.actors[i]
if actor.containsPoint(px, py) then
actor.hitCount+=1 // Increment hit count for the actor
// If the actor has been hit twice, remove it and add a new actor
if (actor.hitCount >= 2) then
this.actors.splice(i, 1)
// Generate random x and y positions for the new actor
local newX = floor(random.nextInt(screen.width ) - screen.width / 2)
local newY = floor(random.nextInt(screen.height ) - screen.height / 2)
local newActor = new Actor(newX, newY, 50, 50, sprites["icon"])
this.actors.push(newActor)
end
end
end
end
drawActors = function()
this.actors.forEach( function( actor )
actor.draw()
end)
end
end
init = function()
actors = new ActorsCollection()
end
update = function()
if mouse.press then
actors.checkPoint( mouse.x, mouse.y )
end
end
draw = function()
screen.clear()
actors.drawActors()
end
It doesn’t work. If it helps this is the current code I‘ve got:
init = function()
cherry = new Sprite("cherry")
diamond = new Sprite("diamond")
star = new Sprite("star")
bell = new Sprite("bell")
seven = new Sprite("seven")
Sprites = ["cherry", "diamond", "star", "bell", "seven"]
slots = ["", "", ""]
message = "Tap the screen to play!"
screen.clear()
screen.drawMap("map", 0, 0, 320, 200)
screen.drawText(message, 0, 85, 20, "")
end
draw = function()
for t in touch.touches
screen.drawSprite(randomSprite(), 0, 0, 70, 70)
screen.drawSprite(randomSprite(), 120, 0, 60, 60)
screen.drawSprite(randomSprite(), -120, 0, 70, 70)
end
end
randomSprite = function()
return Sprites[floor(random.next() * Sprites.length)]
end
playGame = function()
slots[0] = random.nextSprite()
slots[1] = random.nextSprite()
slots[2] = random.nextSprite()
end
if randomSprite() == randomSprite() and randomSprite() == randomSprite() then
message = "💰 JACKPOT! You won! 💰"
else if randomSprite() == randomSprite() or randomSprite() == randomSprite() or randomSprite() == randomSprite() then
message = "💸 Small reward! Two symbols match. 💸"
else
message = " You lost! Try again."
end
end
touchStart = function()
end
update = function()
end