I am trying to make a game where you can draw in a pattern and is it matchs with any of the patterns in the list, something else happens.
Thanks to Ignatz, I have fixed up the last error, but it still is not working as I want it to.
I edited out all the other parts, but my code is this:
-- Play
MainScene=class()
------------------------------------------------------------------------------------------------------------------------------------------------
--for spells, 1 name, 2 attack point, then numbers
--{0,0,0, 0,0,0, 0,0,0, 0,0,0, 0,0,0, 0,0,0, 0,0,0, 0,0,0, 0,0,0, 0,0,0, 0,0,0, 0,0,0, 0,0,0, 0}
Spells={
{"Spell number one", 50, "4, 5, 6, 9"},
{"Spell number two", 20, "1, 2, 3"},
{"Spell number three", 100, "6, 5, 2"},
{"Spell number four", 70, "1,2"}
}
--Written by Ignatz on Mar.9
------------------------------------------------------------------------
local touchSeq={}
local spells
local backButton
local touchCount
local castASpell={}
local number
local lastTouched
local smallDots={}
local dotsPos={}
local spellCasted={}
local thing
local enemies={}
local enemyHealths={}
local enemyHealth
local enemyAttackPoint
local enemyDefeated
local enemyAttacked
local pointsEarned
local timePassed
local yesSpell
function MainScene:init()
thing= SpriteObject("Tyrian Remastered:Dark Orb", vec2(0,0))--touched spot identifier
pointsEarned=0
enemyAttackPoint=0
lastTouched=0
timePassed=ElapsedTime
--Written by Ignatz on Mar.9
--make an extra table where the lookup key is the touch sequence
--so touchSeq["4, 2, 8, 7"]=3
for i=1, #Spells do
touchSeq[Spells[i][3]] = i
end
dotsPos={ vec2(WIDTH/2-WIDTH/4.5, HEIGHT/2+WIDTH/4.5),
vec2(WIDTH/2,HEIGHT/2+WIDTH/4.5),
vec2(WIDTH/2+WIDTH/4.5,HEIGHT/2+WIDTH/4.5),
vec2(WIDTH/2-WIDTH/4.5,HEIGHT/2),
vec2(WIDTH/2,HEIGHT/2),
vec2(WIDTH/2+WIDTH/4.5,HEIGHT/2),
vec2(WIDTH/2-WIDTH/4.5,HEIGHT/2-WIDTH/4.5),
vec2(WIDTH/2,HEIGHT/2-WIDTH/4.5),
vec2(WIDTH/2+WIDTH/4.5,HEIGHT/2-WIDTH/4.5)}--position of dot sprites
castASpell={}--set it to zero
spellCasted={}--set it to zero
enemies={SpriteObject("Documents:u", vec2(WIDTH/2+100, HEIGHT/2+250))}
for i=1, 20 do--set enemy numbers and healths
table.insert(enemies, i, i)
table.insert(enemyHealths,i, i*50)
end
touchCount=1
yesSpell=false
enemyDefeated=true
enemyAttacked=false
for i=1,9 do
table.insert(smallDots, i, SpriteObject("Tyrian Remastered:Brown Capsule 3", dotsPos[i]))
smallDots[i].draggable=false
end
end
------------------------------------------------------------------------------------------------------------------------------------------------
function MainScene:draw()
background(4, 4, 4, 255)
if spriteEnemies==true then--background differs for each mode
sprite("Cargo Bot:Starry Background", WIDTH/2, HEIGHT/2, WIDTH, HEIGHT)
else
sprite("Platformer Art:Block Brick", WIDTH/2, HEIGHT/2, WIDTH, HEIGHT)
end
if timePassed+2.7<ElapsedTime then--sprite enemy
enemyDefeated=false
end
if enemyDefeated==false then--if enemy is not dead
if enemyAttackPoint~=0 then
tint(165, 24, 24, 255)
ElapsedTime=0
attacked= ElapsedTime
enemyHealth=enemyHealth-enemyAttackPoint
enemyAttackPoint=0
if enemyHealth<=0 then
enemyDefeated=true
timePassed=ElapsedTime
end
end
end
if enemyDefeated==true then --save housepoints
if house== 1 then
housePoints=saveLocalData("gp", pointsEarned)
elseif house== 2 then
housePoints=saveLocalData("rp", pointsEarned)
elseif house== 3 then
housePoints=saveLocalData("hp", pointsEarned)
else
housePoints=saveLocalData("sp", pointsEarned)
end
pointsEarned=0
timePassed= ElapsedTime
end
tint(153, 153, 153, 255)
thing:draw()
noTint()
for i=1,9 do--draw dots
tint(87, 87, 87, 180)
smallDots[i]:draw()
end
fontSize (60)
fill(236, 209, 65, 255)
font("Verdana-Bold")
textMode (CENTER)
pushStyle ()
end
------------------------------------------------------------------------------------------------------------------------------------------------
function MainScene:touched(touch)
if enemyDefeated==false then--enemy fighting scene
Scene.Change("enemy")
end
if touch.state==BEGAN or touch.state== MOVING then--move object
thing= SpriteObject("Tyrian Remastered:Dark Orb", vec2(CurrentTouch.x, CurrentTouch.y))
else
castASpell=spellCasted
yesSpell=true
print(table.unpack(castASpell))
spellCasted={}
thing= SpriteObject("Tyrian Remastered:Dark Orb",vec2(0,0))
touchCount=1
end
for n=1,9 do --put in numbers if touched
if (thing:isTouching(smallDots[n])==true) then
if (lastTouched~=n) then
c= math.random(1,4)--sound
if c==1 then
sound("A Hero's Quest:Swing 1")
elseif c==2 then
sound("A Hero's Quest:Swing 2")
elseif c==3 then
sound("A Hero's Quest:Swing 3")
else
sound("A Hero's Quest:Swing 4")
end
table.insert(spellCasted,touchCount, n)
print(spellCasted[touchCount])
lastTouched=n
touchCount= touchCount+1
end
end
end
------------------------------------------------------------------------------------------------------------------------------------------------
if yesSpell==true then
--Written by Ignatz on Mar.9
--turn your table of touches into a comma separated string
--so {1,2,3,4} becomes "1,2,3,4"
print(table.concat(castASpell,", "))
local c = table.concat(castASpell,", ")
--look it up in our new table, if it isn't found, it returns nil
pattern = touchSeq[c] --just one lookup is needed!
--and when we want to use it later
if pattern then --if a pattern has been chosen
enemyAttackPoint=Spells[pattern][2]
theSpellThatIsCasted= Spells[pattern][1]
print(table.unpack(theSpellThatIsCasted))
end
yesSpell=false
end
--this is good is if your Spells table has lots of items
--your code above would require you to copy all of them into variables
--whereas this approach just stores one number (pattern) for the chosen
--spell, and when you want to use one of the items, you just look it up
end
-- Main
-- Scene change
--By hohohohoho
house=nil
enemyDefeated=true
gryfindorPoints=nil
slytherinPoints=nil
hufflepuffPoints=nil
ravenclawPoints=nil
song=nil
function setup()
supportedOrientations(PORTRAIT_ANY)
displayMode(FULLSCREEN)
noFill()
noSmooth()
noStroke()
pushStyle()
song=""
gryfindorPoints=readLocalData("gp", 0)
slytherinPoints=readLocalData("sp", 0)
hufflepuffPoints=readLocalData("hp", 0)
ravenclawPoints=readLocalData("rp", 0)
music(song, true, 0.3)
house=readLocalData("house", 1)
new=h
Scene("play", MainScene)
--?? ???? ?? ??? ??.
Scene.Change("play")
end
function draw()
background(0, 0, 0, 255)
Scene:Draw()
end
--Touched needed in the main.
function touched(touch)
Scene.Touched(touch)
end
-- HelperClass
-- Created by: Mr Coxall
-- Created on: Nov 2014
-- Created for: ICS2O
-- This is a collection of classes and a scene management system,
-- to help in the aid of coding.
-- Button Class
-- This class simplifies the process of checking if a sprite button is pressed or not
-- Then you can ask the button if it is being touched or was ended.
-- Works best with vector sprite buttons, since it enlarges them when they are touched
--
-- Parameters to pass in:
-- 1. Untouched button image name
-- 2. A vector2 that is the location of the button
Button = class()
function Button:init(buttonImage, buttonPosition, buttonScaleSize)
-- accepts the button image, location and scale to draw it
self.buttonImage = buttonImage
self.buttonLocation = buttonPosition
self.buttonScaleSize = buttonScaleSize or 1.0
self.buttonImageSize = vec2(spriteSize(self.buttonImage))
-- resize the button to new size (or scale by 1.0 if no scale given!)
if not(self.buttonScaleSize == 1.0) then
self.buttonImage = resizeImage(self.buttonImage,(self.buttonScaleSize*self.buttonImageSize.x),(self.buttonScaleSize*self.buttonImageSize.y))
-- now rest button image size
self.buttonImageSize = vec2(spriteSize(self.buttonImage))
end
self.buttonTouchScale = 1.15
--self.buttonImageSize = vec2(spriteSize(self.buttonImage))
self.currentButtonImage = self.buttonImage
self.buttonTouchedImage = resizeImage(self.buttonImage, (self.buttonImageSize.x*self.buttonTouchScale), (self.buttonImageSize.y*self.buttonTouchScale))
self.selected = false
end
function Button:draw()
-- Codea does not automatically call this method
pushStyle()
pushMatrix()
noFill()
noSmooth()
noStroke()
sprite(self.currentButtonImage, self.buttonLocation.x, self.buttonLocation.y)
popMatrix()
popStyle()
end
function Button:touched(touch)
-- local varaibles
local currentTouchPosition = vec2(touch.x, touch.y)
-- reset touching variable to false
self.selected = false
if (touch.state == BEGAN) then
if( (self.buttonLocation.x - self.buttonImageSize.x/2) < currentTouchPosition.x and
(self.buttonLocation.x + self.buttonImageSize.x/2) > currentTouchPosition.x and
(self.buttonLocation.y - self.buttonImageSize.y/2) < currentTouchPosition.y and
(self.buttonLocation.y + self.buttonImageSize.y/2) > currentTouchPosition.y ) then
self.currentButtonImage = self.buttonTouchedImage
--print("Now touching! - began")
else
self.currentButtonImage = self.buttonImage
--print("Not touching - began")
end
end
if (touch.state == MOVING) then
if( (self.buttonLocation.x - self.buttonImageSize.x/2) < currentTouchPosition.x and
(self.buttonLocation.x + self.buttonImageSize.x/2) > currentTouchPosition.x and
(self.buttonLocation.y - self.buttonImageSize.y/2) < currentTouchPosition.y and
(self.buttonLocation.y + self.buttonImageSize.y/2) > currentTouchPosition.y ) then
self.currentButtonImage = self.buttonTouchedImage
--print("Now touching! - moving")
else
self.currentButtonImage = self.buttonImage
--print("Not touching - moving")
end
end
if (touch.state == ENDED) then
if( (self.buttonLocation.x - self.buttonImageSize.x/2) < currentTouchPosition.x and
(self.buttonLocation.x + self.buttonImageSize.x/2) > currentTouchPosition.x and
(self.buttonLocation.y - self.buttonImageSize.y/2) < currentTouchPosition.y and
(self.buttonLocation.y + self.buttonImageSize.y/2) > currentTouchPosition.y ) then
self.selected = true
--print("Activated button")
end
self.currentButtonImage = self.buttonImage
end
end
function resizeImage(img, width, height)
-- function from
-- http://codea.io/talk/discussion/3490/importing-pics-from-dropbox/p1
local newImg = image(width,height)
setContext(newImg)
sprite( img, width/2, height/2, width, height )
setContext()
return newImg
end
-- Dragging Object Class
-- This class simplifies the process of dragging and dropping objects
-- You can have several objects interacting, but it is not mulit-touch
--
-- Parameters to pass in:
-- 1. Object image name
-- 2. A vector2 that is the location of the button
-- 3. Optional object id
SpriteObject = class()
function SpriteObject:init(objectImage, objectStartPosition, objectID)
self.objectImage = objectImage
self.objectStartLocation = objectStartPosition
self.ID = objectID or math.random()
self.objectCurrentLocation = self.objectStartLocation
self.objectImageSize = vec2(spriteSize(self.objectImage))
--self.selected = false
self.dragOffset = vec2(0,0)
self.draggable = true
-- yes, the following does need to be global to the entire program
-- this is the only way (easy way!) to move things around and no get
-- several object "attached" to each other
-- this way just the top object in the stack moves
DRAGGING_OBJECT_MOVING = nil or DRAGGING_OBJECT_MOVING
end
function SpriteObject:draw()
-- Codea does not automatically call this method
pushStyle()
pushMatrix()
noFill()
noSmooth()
noStroke()
sprite(self.objectImage, self.objectCurrentLocation.x, self.objectCurrentLocation.y)
popMatrix()
popStyle()
end
function SpriteObject:touched(touch)
-- Codea does not automatically call this method
-- local varaibles
local currentTouchPosition = vec2(touch.x, touch.y)
if (touch.state == BEGAN and self.draggable == true) then
if( (self.objectCurrentLocation.x - self.objectImageSize.x/2) < currentTouchPosition.x and
(self.objectCurrentLocation.x + self.objectImageSize.x/2) > currentTouchPosition.x and
(self.objectCurrentLocation.y - self.objectImageSize.y/2) < currentTouchPosition.y and
(self.objectCurrentLocation.y + self.objectImageSize.y/2) > currentTouchPosition.y ) then
-- if the touch has began, we need to find delta from touch to center of object
-- since will need it to reposition the object for draw
-- subtracting 2 vec2s here
self.dragOffset = self.objectCurrentLocation - currentTouchPosition
DRAGGING_OBJECT_MOVING = self.ID
end
end
if (touch.state == MOVING and self.draggable == true) then
if( (self.objectCurrentLocation.x - self.objectImageSize.x/2) < currentTouchPosition.x and
(self.objectCurrentLocation.x + self.objectImageSize.x/2) > currentTouchPosition.x and
(self.objectCurrentLocation.y - self.objectImageSize.y/2) < currentTouchPosition.y and
(self.objectCurrentLocation.y + self.objectImageSize.y/2) > currentTouchPosition.y ) then
-- only let it move if self.draggable == true
if (self.draggable == true) then
-- add the offset back in for its new position
if (self.ID == DRAGGING_OBJECT_MOVING) then
self.objectCurrentLocation = currentTouchPosition + self.dragOffset
end
end
end
end
if (touch.state == ENDED and self.draggable == true) then
DRAGGING_OBJECT_MOVING = nil
end
end
function SpriteObject:isTouching(otherSpriteObject)
-- this method checks if one dragging object is touching another dragging object
local isItTouching = false
if( (self.objectCurrentLocation.x + self.objectImageSize.x/2) > (otherSpriteObject.objectCurrentLocation.x - otherSpriteObject.objectImageSize.x/2) and
(self.objectCurrentLocation.x - self.objectImageSize.x/2) < (otherSpriteObject.objectCurrentLocation.x + otherSpriteObject.objectImageSize.x/2) and
(self.objectCurrentLocation.y - self.objectImageSize.y/2) < (otherSpriteObject.objectCurrentLocation.y + otherSpriteObject.objectImageSize.y/2) and
(self.objectCurrentLocation.y + self.objectImageSize.y/2) > (otherSpriteObject.objectCurrentLocation.y - otherSpriteObject.objectImageSize.y/2) ) then
-- if true, then not touching
isItTouching = true
end
return isItTouching
end
-- SceneManager
--
-- This file lets you easily manage different scenes
-- Original code from Brainfox, off the Codea forums
Scene = {}
local scenes = {}
local sceneNames = {}
local currentScene = nil
setmetatable(Scene,{__call = function(_,name,cls)
if (not currentScene) then
currentScene = 1
end
table.insert(scenes,cls)
sceneNames[name] = #scenes
Scene_Select = nil
end})
--Change scene
Scene.Change = function(name)
currentScene = sceneNames[name]
scenes[currentScene]:init()
if (Scene_Select) then
Scene_Select = currentScene
end
collectgarbage()
end
Scene.Draw = function()
pushStyle()
pushMatrix()
scenes[currentScene]:draw()
popMatrix()
popStyle()
end
Scene.Touched = function(t)
if (scenes[currentScene].touched) then
scenes[currentScene]:touched(t)
end
end
Scene.Keyboard = function()
if (scenes[currentScene].keyboard) then
scenes[currentScene]:keyboard(key)
end
end
Scene.OrientationChanged = function()
if (scenes[currentScene].orientationChanged) then
scenes[currentScene]:orientationChanged()
end
end
@hohohohoho When you post code, put 3~'s on a line before and after your code so it shows properly. I add them to your code above.
Well, for such a long chunk of code as that Iād recommend exporting it using Codeaās āCopy Project Codeā function (long-press on project > āExportā > āCopy Project Codeā), then posting it as a single block in a code hosting site, such as github gist. Then people can copy it in one go, and import it into Codea with "Paste into Project) (long-press on ā+ new porojectā button > paste-into-project). Otherwise people would have to go back and forth several times from this page, copying and pasting into tabs etc.
@hohohohoho Instead of me looking thru all of your code and trying to figure out what youāre doing wrong, you can look thru my code and see what Iām doing right. I think this is what youāre trying to do, if not let me know and Iāll modify it. Iām using numbers in the buttons to make it easy to see where to move and I donāt use a string for the number pattern in the spells table.
function setup()
spells={{"spell 1",50,4569},
{"spell 2",20,123},
{"spell 3",100,652},
{"spell 4",70,12},
{"spell 5",30,1236987}}
btn={}
z=0
for x=1,3 do
for y=1,3 do
z=z+1
table.insert(btn,button(x*80,y*80,75,75,z))
end
end
end
function draw()
background(40, 40, 50)
for a,b in pairs(btn) do
b:draw()
end
end
function touched(t)
if t.state==BEGAN then
str=0
end
if t.state==MOVING then
for a,b in pairs(btn) do
b:touched(t)
end
end
if t.state==ENDED then
for z=1,#spells do
if str==spells[z][3] then
print(spells[z][1])
break
end
end
end
end
button=class()
function button:init(x,y,w,h,v)
self.x=x
self.y=y
self.w=w
self.h=h
self.v=v
end
function button:draw()
fill(255)
rect(self.x,self.y,self.w,self.h)
fill(0)
text(self.v,self.x+40,self.y+40)
end
function button:touched(t)
if t.x>self.x and t.x<self.x+self.w and
t.y>self.y and t.y<self.y+self.h then
if self.v~=last then
str=str*10+self.v
last=self.v
end
end
end