Results 1 to 8 of 8

Thread: Lua help

                  
   
  1. #1

    Default Lua help

    Hi, I've just started learning Lua and need someone to go over my code because i keep getting an error.
    The error is:

    LuaScript.Lua:15:attempt to index global 'control' (a nil value)

    Here is my Script:

    red = Color.new(255,0,0)

    player = Image.createEmpty(35,10)
    player:clear(red)

    playerHeight = 10
    playerWidth = 35

    while true do

    screen:clear()

    screen:blit(240, 11, player)

    pad = Control.read()

    if pad:left()then
    PLayer.x = Player.x - 1
    end

    if pad:right() then
    Player.x = Player.x + 1
    end

    screen.waitVblankStart()
    screen.flip()
    end

    If there are any other problems with my Script can you post them. Thanks in advance.

  2. #2
    DCEmu Old Pro
    Join Date
    May 2006
    Posts
    1,073
    Rep Power
    71

    Default

    analysis
    LuaScript.Lua:15 - this means error is on line 15

    line 15 - pad = Control.read()
    it should be pad = Controls.read()

    problem now is you get errors when you press left or right

  3. #3

    Default

    Thanks for the help but what can i do to get my left and right buttons to work?

  4. #4
    DCEmu Regular Nicko01's Avatar
    Join Date
    Jun 2006
    Location
    www.nicko01.com
    Age
    31
    Posts
    434
    Rep Power
    70

    Default

    try

    "if pad:left() then
    player.x = player.x - 1
    end"

    I think caps mess it up and there needs to be a space after the "()" before "then"

  5. #5
    DCEmu Regular Nicko01's Avatar
    Join Date
    Jun 2006
    Location
    www.nicko01.com
    Age
    31
    Posts
    434
    Rep Power
    70

    Default

    Heres 1 with 2 players
    (you need the images or it won't work.)

    grass = Image.load("grass.png")
    player = Image.load("player.png")
    flower = Image.load("flower.png")
    screenwidth = 480 - player:width()
    screenheight = 272 - player:width()
    Player = { }
    Player[1] = { x = 200, y = 50 }
    Player[2] = { x = 125, y = 20
    while true do
    pad = Controls.read()
    screen:clear()
    for a = 0, 14 do
    for b = 0,8 do
    screen:blit(32 * a, 32 * b, grass)
    end
    end
    screen:blit(100,100,flower)
    screen:blit(300,220,flower)
    screen:blit(Player[1].x,Player[1].y,player)
    screen:blit(Player[2].x,Player[2].y,player)
    if pad:left() and Player[1].x > 0 then
    Player[1].x = Player[1].x - 2
    end

    if pad:right() and Player[1].x < screenwidth then
    Player[1].x = Player[1].x + 2
    end

    if pad:up() and Player[1].y > 0 then
    Player[1].y = Player[1].y - 2
    end

    if pad:down() and Player[1].y < screenheight then
    Player[1].y = Player[1].y + 2
    end

    if pad:square() and Player[2].x > 0 then
    Player[2].x = Player[2].x - 2
    end

    if pad:circle() and Player[2].x < screenwidth then
    Player[2].x = Player[2].x + 2
    end

    if pad:triangle() and Player[2].y > 0 then
    Player[2].y = Player[2].y - 2
    end

    if pad:cross() and Player[2].y < screenheight then
    Player[2].y = Player[2].y + 2
    end
    screen.waitVblankStart()
    screen.flip()
    end

  6. #6

    Default

    Is there a way to get my left and right to work without having to load the image?

    The error is: LuaScript.Lua :18: attempt to index global 'Player' (a nil value)

  7. #7
    GP2X Coder/Moderator
    Join Date
    Jan 2006
    Posts
    1,678
    Rep Power
    83

    Default

    You can change the image loading with full coloured squares like you did before.

  8. #8
    DCEmu Old Pro
    Join Date
    May 2006
    Posts
    1,073
    Rep Power
    71

    Default

    heres your code rewritten and working on lua player 0.17dk2. dont see why it shouldnt workon other versions

    red = Color.new(255,0,0)
    PlayerWidth = 35
    PlayerHeight = 10
    Player = Image.createEmpty(PlayerWidth,PlayerHeight)
    Player:clear(red
    Playerx = 240
    Playery = 11

    while true do
    screen:clear()
    screen:blit(Playerx,Playery,Player)
    pad = Controls.read()
    --Digital Pad Movement
    if pad:left() then
    Playerx = Playerx - 1
    end
    if pad:right() then
    Playerx = Playerx + 1
    end
    if pad:up() then
    Playery = Playery - 1
    end
    if pad:down() then
    Playery = Playery + 1
    end
    --Analogue Pad Movement
    if pad:analogY() < -35 then
    Playery = Playery - 5
    end
    if pad:analogY() > 35 then
    Playery = Playery + 5
    end
    if pad:analogX() < -36 then
    Playerx = Playerx - 5
    end
    if pad:analogX() > 35 then
    Playerx = Playerx + 5
    end
    --Collision
    if Playerx < 0 then
    Playerx = 0
    end
    if Playerx > 480 - PlayerWidth then
    Playerx = 480 - PlayerWidth
    end
    if Playery < 0 then
    Playery = 0
    end
    if Playery > 272 - PlayerHeight then
    Playery = 272 - PlayerHeight
    end
    --End game
    if pad:select() then break
    end
    screen.waitVblankStart()
    screen.flip()
    end

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •