Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

[HELP] I have an error in a basic function

edited April 2014 in Questions Posts: 116

Hi i am very bad at Codea and Lua but i am trying to make my project work. Today i had an error and i dont know, what should i do. If you can good Lua and you are kind, please help! Thanks a lot for your attention!
PS Sorry for bad English!
here is the code:


--# Main -- Block Gravity 2 -- 15.04.14 function setup() parameter.boolean("OrientationParameter", true) orientation = true --[ true {_} Horizontal ]----[ false {_} Vertical ]-- Character = Char(300,100,100,100) Ground = Object(0,0,WIDTH, 100) end function draw() orientation = OrientetionParameter Character:draw() Ground:draw() if CurrentTouch.state == BEGAN or CurrentTouch.state == MOVING then Character:touch() end if orientation == true then if Character:bottom() > Ground:top() - Ground.height / 2 then Character.posObj = "down" elseif Character:top() < Ground:bottom() + Ground.height / 2 then Character.posObj = "up" end elseif orientation == false then if Character:left() > Ground:right() - Ground.width / 2 then Character.posObj = "left" elseif Character:right() < Ground:left() + Ground.width / 2 then Character.posObj = "right" end end end --# Object Object = class() function Object:init(x,y,width,height) self.x = x self.y = y self.width = width self.height = height end function Object:draw() -- Draw a Rect rectMode(CORNER) rect(self.x, self.y,self.width,self.height) fill(255, 255, 255, 255) strokeWidth = 0 end function Object:top() return self.y + self.height end function Object:bottom() return self.y end function Object:left() return self.x end function Object:right() return self.x + self.width end --# Char Char = class() function Char:init(x,y,width,height) self.x = x self.y = y self.width = width self.height = height self.touched = false self.speed = 100 self.acc = 10 self.posObj = "" -- where is the ground from viewpoint of char self.jumpDir = 0 -- Jump Direction end function Char:draw() -- Draw a Rect rectMode(CORNER) rect(self.x, self.y,self.width,self.height) fill(255, 255, 255, 255) strokeWidth = 0 -- Collision Char:collision(Ground) -- Jump if self.touched == true then Char:jump() end end function Char:touch() self.touched = true end function Char:jump() if Orientation == true then -- Setting the direction of Jump if self.posObj == "down" then self.jumpDir = 1 elseif self.posObj == "up" then self.jumpDir = -1 end -- Jumping self.speed = self.posObj * (self.speed - self.acc) self.y = self.y + self.speed else -- Similar to the upper Jump if self.posObj == "left" then self.jumpDir = 1 elseif self.posObj == "right" then self.jumpDir = -1 end self.speed = self.posObj * (self.speed - self.acc) self.x = self.x + self.speed end end function Char:top() return self.y + self.height end function Char:bottom() return self.y end function Char:left() return self.x end function Char:right() return self.x + self.width end function Char:collision(obj) -- is Char between left and right corners of obj? if Char:right() > obj:left() and Char:left() < obj:right() then if orientation == true then -- Char is on obj and colliding if self.posObj == "down" and Char:bottom() < obj:top() then self.y = obj:top() print("Collision") self.speed = 100 self.touched = false -- Char is under obj and colliding elseif self.posObj == "up" and Char:top() > obj:bottom() then self.y = obj:bottom() - self.width print("Collision") self.speed = 100 self.touched = false end end end -- is Char between top and bottom corners of obj? if Char:top() < obj:bottom() and Char:bottom() > obj:top() then if orientation == false then -- Char is right from obj and colliding if self.posObj == "left" and Char:left() < obj:right() then self.x = obj:right() print("Collision") self.speed = 100 self.touched = false -- Char is left from obj and colliding elseif self.posObj == "right" and Char:right() > obj:left() then self.x = obj:left() - self.height print("Collision") self.speed = 100 self.touched = false end end end end
Tagged:

Comments

  • dave1707dave1707 Mod
    edited April 2014 Posts: 7,809

    @TimurEke In your Char class, anytime you call a function in the Char class, change Char to self. For example in Character:draw() you call Char:collision(Ground). It should be self:collision(Ground). Same for all other the other Char:function calls. Also, your self.posObj in Char:init might need to be changed from " " to 0.

    EDIT: Don't change any of your "function Char:name..." functions.

  • edited April 2014 Posts: 425

    @TimurEke when you want to call

    Char:collision()
    

    or

    Char:left()
    

    From inside the Char class (tab) you need to use

    self:left()
    

    or

    self:collision()
    

    To tell Char which instance of Char is calling

    :left() 
    

    in this case it is self

    Sorry if I haven't explained it very well.

    Also on line 50 in the Char class you seem to be trying to do

    self.speed = self.posObj * (self.speed - self.acc)
    

    But self.posObj is a string which cannot be used in math opperatios. To change a string to a number you can use

    tonumber("10")
    or
    tonumber(self.posObj)
    
Sign In or Register to comment.