Howdy, Stranger!

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

I can't get this to work at all

edited March 2013 in General Posts: 1

So I thought I was getting the hang and understanding a little now I can't do crap from new project I'm getting really frustrated I can't get anything to show up on screen when I run. I'm trying to simply creat two circles and have them rotate around each other.

Circle one rotate around circle two when you tap the screen they switch cir 2 rotates around cir 1. Here is my code so far

Main class:

function setup()
    function createCircle(x,y,r)
    local circle = physics.body(CIRCLE, r)
    -- enable smooth motion
    circle.interpolate = true
    circle.x = x
    circle.y = y
    circle.restitution = 25
    circle.sleepingAllowed = false
    return circle

Then circle class:

Circle = class()
function Circle:init(x, y, size)
    -- you can accept and set parameters here
    self.x = WIDTH/2
    self.y = HEIGHT/2
    self.size = 25
function Circle:draw()
    ellipse(self.x, self.y, self.size)
    -- Codea does not automatically call this method
function Circle:touched(touch)
    -- Codea does not automatically call this method

Please help what am I doing wrong lol


  • edited March 2013 Posts: 835

    It will greatly help ppl copy ur code if u put -- in front of your notes and put it between a pair of fences. Fences look like:


    --Code Here


  • edited March 2013 Posts: 182

    where in main do you call the Circle class?
    And you know the create circle function doesnt run itself.
    Just a sec fixing up your code

  • Posts: 707

    .@DeanC1985 your circle class is good but you need to call it from your main program to initialise and draw it. Getting the circles to rotate round each other was a bit tricky but the following should work. I included a basic tap control and the option for you to modify to anticlockwise rotation if you wish

    --# Circle Circle = class() function Circle:init(x,y,r,state) -- you can accept and set parameters here self.x = x self.y=y self.size=r self.state=state self.rotspeed=0.02 end function Circle:draw(p) self.p=p ellipse(self.x,self.y,self.size) local dist=math.sqrt(((self.x-self.p.x)^2)+((self.y-self.p.y)^2)) local angle=math.atan2((self.x-self.p.x),(self.y-self.p.y)) if self.state==CLOCKWISE then self.x = self.p.x + dist*math.sin(angle+self.rotspeed) self.y = self.p.y + dist*math.cos(angle+self.rotspeed) end end function Circle:touched(touch) -- Codea does not automatically call this method end


    --# Main -- circle demo -- Use this function to perform your initial setup function setup() STATIONARY=0 CLOCKWISE=1 ANTICLOCKWISE=2 c1=Circle(WIDTH/2,HEIGHT/2,50,STATIONARY) c2=Circle(WIDTH/4,HEIGHT/2,50,CLOCKWISE) timer=0 touches={} end -- This function gets called once every frame function draw() -- This sets a dark background color background(40, 40, 50) c1:draw(c2) c2:draw(c1) for k,touch in pairs(touches) do -- Use the touch id as the random seed if (math.abs(touch.x-c1.x)<20 and math.abs(touch.y-c1.y)) or (math.abs(touch.x-c2.x)<20 and math.abs(touch.y-c2.y)) then if c2.state==STATIONARY then c2.state=CLOCKWISE c1.state=STATIONARY else c1.state=CLOCKWISE c2.state=STATIONARY end touches[k] = nil end end end function touched(touch) if touch.state == ENDED then touches[] = nil else touches[] = touch end end
Sign In or Register to comment.