Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat
Quick questions about Gideros&Lua - Page 3 - Gideros Forum

Quick questions about Gideros&Lua

13

Comments

  • Apollo14Apollo14 Member
    edited February 2
    That is awesome! I will use constants wherever I can! :)

    I'm looking at Lua's table manipulation functions.
    table.pack doesn't work in Gideros?
    myTab = {1,2,3,"four"}
    print(table.pack(myTab)) --spawns error message
  • Apollo14Apollo14 Member
    edited February 2
    Guys! I don't get how does "setVolume" work.
    Let's say we want to play sound louder and louder with every click.
    I expected something like this, but of course it doesn't work:
    currVolumeVar = 0
    newSound = Sound.new("audio/piano_a.wav")
    newSound:setVolume(currVolumeVar)
     
    co = coroutine.create(function()
    	for i=1, 10 do
    	print("current volume:",currVolumeVar)
    	newSound:play()
    	currVolumeVar += 0.1
    	newSound:setVolume(currVolumeVar)
    	coroutine.yield()
    	end
    end)
     
    function onMouseDownFunc()
    	coroutine.resume(co)
    end
     
    stage:addEventListener(Event.MOUSE_DOWN, onMouseDownFunc)
    (I didn't find examples how to work with SoundChannel)
  • hgy29hgy29 Maintainer
    Try this:
    currVolumeVar = 0
    newSound = Sound.new("audio/piano_a.wav")
     
    co = coroutine.create(function()
    	for i=1, 10 do
            currVolumeVar += 0.1
    	print("current volume:",currVolumeVar)
    	local channel= newSound:play()
    	channel:setVolume(currVolumeVar)
    	coroutine.yield()
    	end
    end)
     
    function onMouseDownFunc()
    	coroutine.resume(co)
    end
     
    stage:addEventListener(Event.MOUSE_DOWN, onMouseDownFunc)

    Likes: Apollo14

    +1 -1 (+1 / -0 ) Share on Facebook
  • Apollo14Apollo14 Member
    edited February 4
    Guys, how do we usually stop a fake thread like this? Is it even stoppable?
    function pulse()
      local i=0
     
      while (true) do
        pulsingSprite:setAlpha((math.sin(i))^2)
        i=i+0.1
        Core.yield(true)
      end
    end
     
    Core.asyncCall(pulse)
     
    function onMouseDownFunc()
    	--some command to stop pulsing
    end
     
    stage:addEventListener(Event.MOUSE_DOWN, onMouseDownFunc)
  • hgy29hgy29 Maintainer
    use a global or upvalue as a condition to exit your while loop:
    local pulseRunning=true
    function pulse()
      local i=0
     
      while (pulseRunning) do
        pulsingSprite:setAlpha((math.sin(i))^2)
        i=i+0.1
        Core.yield(true)
      end
    end
     
    Core.asyncCall(pulse)
     
    function onMouseDownFunc()
    	-- pulse thread will exit at next iteration
            pulseRunning=false
    end
     
    stage:addEventListener(Event.MOUSE_DOWN, onMouseDownFunc)

    Likes: Apollo14, antix

    +1 -1 (+2 / -0 ) Share on Facebook
  • How to get current frame while MovieClip is playing? Something like:
    myAnim = MovieClip.new{
    {1, 10, myTable[1]},
    {10, 20, myTable[2]},
    {20, 30, myTable[3]},
    }
    --let's say animation started and we wanna get number of current frame:
    currentFrameVar = myAnim:getCurrentFrame()
    (I didn't find in documentation)
  • olegoleg Member
    edited February 5
    alternative
    myAnim = MovieClip.new{
    {1, 10, myTable[1]},
    {10, 20, myTable[2]},
    {20, 30, myTable[3]},
    }
    --let's say animation started and we wanna get number of current frame:
    currentFrameVar = 30
    myAnim:gotoAndStop(currentFrameVar )

    Likes: Apollo14

    +1 -1 (+1 / -0 ) Share on Facebook
  • MovieClip:getFrame() was added a while back, but not yet added to docs.

    Likes: Apollo14

    My Gideros games: www.totebo.com
    +1 -1 (+1 / -0 ) Share on Facebook
  • olegoleg Member
    @totebo GiderosApi.pdf is automatically generated - why not there ??
  • I don't think it's added to the actual docs. I've made a start of adding it, but not quite sure how to do it.
    My Gideros games: www.totebo.com
  • antixantix Member
    edited February 6
    @totebo, the docs are the xml files in the docsrc folder which are edited manually by @hgy29. He then syncs these to the MySQL database.. and then there is a script that converts them into the actual documents. That is my understanding anyway.

    I have created a Gideros program that converts all of the XML files into Lua Tables, and then encodes them into JSON strings.

    I have been trying to create an editor for these JSON strings but it appears there is no way to accomplish this in a web browser, no way to accomplish this in Gideros (because like no access to ENTER or DELETE keys etc in KeyCode) and currently trying to deserialize the JSON strings in VB.NET has sucked days from my life :(

    I am getting near the point where I think I should throw JSON out the window (and VB.NET) and store everything in a simple text format that is easy to edit.
    Follow me on FaceBook Check out my DevBlog, my GitHub, and try my games...
    Falling Animals | Breaky Wall | Exetor | Mini Putt Golfing
  • Apollo14Apollo14 Member
    edited February 6
    MovieClip:getFrame() really works, thx! :)

    Guys, what undocumented options are available for Texture.new, besides "wrap = Texture.REPEAT"? What do "format" and "extend" options do?
    Texture.new(filename, filtering [, options])
    http://docs.giderosmobile.com/reference/gideros/Texture/new

    Likes: totebo

    +1 -1 (+1 / -0 ) Share on Facebook
  • hgy29hgy29 Maintainer
    extend: Boolean indicating if texture should be extended in memory to the next power of two size, default to true
    format: actual texture format in memory, like RGB, RGBA, Y (greyscale) and so on. Useful when creating textures from byte arrays

    Likes: antix, Apollo14

    +1 -1 (+2 / -0 ) Share on Facebook
  • Author of this article writes this drag&drop function:
    function moveBall(event)
     
        if ball:hitTestPoint(event.x, event.y) then
            local dx = event.x - ball:getX()
            local dy = event.y - ball:getY()
     
            ball:setX(ball:getX() + dx)
            ball:setY(ball:getY() + dy)
            ball.x0 = event.x
            ball.y0 = event.y
     
            event:stopPropagation()
        end
    end
    I don't get why not just set ball's X&Y directly?
    function moveBall(event)
     
        if ball:hitTestPoint(event.x, event.y) then
        ballSprite:setPosition(event.x, event.y)
        event:stopPropagation()
        end
    end
    dragging visually looks same

    How to regulate dragging speed? As it is, ball moves instantly, what if we need it slower.
  • olegoleg Member
    edited February 6
    filter =0.9
     --  ball:setX(ball:getX() + dx)
         --   ball:setY(ball:getY() + dy)
     ball:setX(ball:getX()*filter  + dx*(1-filter ))
            ball:setY(ball:getY()*filter  + dy*(1-filter ))

    Likes: Apollo14

    +1 -1 (+1 / -0 ) Share on Facebook
  • Apollo14Apollo14 Member
    edited February 6
    oleg said:

    filter =0.9

     --  ball:setX(ball:getX() + dx)
         --   ball:setY(ball:getY() + dy)
     ball:setX(ball:getX()*filter  + dx*(1-filter ))
            ball:setY(ball:getY()*filter  + dy*(1-filter ))
    For some reason it doesn't work for me, do you know why? (no errors shown, but the ball's drag&drop is broken.
    function moveBall(event)
    filter=.9
        if ballSprite:hitTestPoint(event.x, event.y) then
    		--ballSprite:setPosition(event.x, event.y)
            local dx = event.x - ballSprite:getX()
            local dy = event.y - ballSprite:getY()
     
            ballSprite:setX(ballSprite:getX()*filter + dx*(1-filter ))
            ballSprite:setY(ballSprite:getY()*filter + dy*(1-filter ))
            ballSprite.x0 = event.x
            ballSprite.y0 = event.y
     
            event:stopPropagation()
        end
    end
    -- when we move the mouse then ball also moves
    ballSprite:addEventListener(Event.MOUSE_MOVE, moveBall)
  • olegoleg Member
    edited February 6
    set filter = 0.3
    filter=0.3
    local function onMouseDown(self, event)
    	if self:hitTestPoint(event.x, event.y) then
    		self.isFocus = true
     
    		self.x0 = event.x
    		self.y0 = event.y
     
    		event:stopPropagation()
    	end
    end
     
    local function onMouseMove(self, event)
    	if self.isFocus then
    		local dx = event.x - self.x0
    		local dy = event.y - self.y0
    		print(self:getX()*filter + dx*(1-filter ),"new")
    		print(self:getX() + dx )
    		self:setX(self:getX() + dx*(1-filter ))
    		self:setY(self:getY() + dy*(1-filter ))
     
    		self.x0 = event.x
    		self.y0 = event.y
     
    		event:stopPropagation()
    	end
    end
  • oleg said:

    set filter = 0.3

    it doesn't work :'(
  • olegoleg Member
    edited February 6
    --[[ 
     
    Drag the shapes around with your mouse or fingers
     
    This code is MIT licensed, see <a href="http://www.opensource.org/licenses/mit-license.php" target="_blank" rel="nofollow">http://www.opensource.org/licenses/mit-license.php</a>
    (C) 2010 - 2011 Gideros Mobile 
     
    ]]
    filter=0.3
    local function onMouseDown(self, event)
    	if self:hitTestPoint(event.x, event.y) then
    		self.isFocus = true
     
    		self.x0 = event.x
    		self.y0 = event.y
     
    		event:stopPropagation()
    	end
    end
     
    local function onMouseMove(self, event)
    	if self.isFocus then
    		local dx = event.x - self.x0
    		local dy = event.y - self.y0
    		print(self:getX()*filter + dx*(1-filter ),"new")
    		print(self:getX() + dx )
    		self:setX(self:getX() + dx*(1-filter ))
    		self:setY(self:getY() + dy*(1-filter ))
     
    		self.x0 = event.x
    		self.y0 = event.y
     
    		event:stopPropagation()
    	end
    end
     
    local function onMouseUp(self, event)
    	if self.isFocus then
    		self.isFocus = false
    		event:stopPropagation()
    	end
    end
     
    for i=1,5 do
    	local shape = Shape.new()
     
    	shape:setLineStyle(3, 0x000000)
    	shape:setFillStyle(Shape.SOLID, 0xff0000, 0.5)
    	shape:beginPath()
    	shape:moveTo(0, 0)
    	shape:lineTo(100, 0)
    	shape:lineTo(100, 50)
    	shape:lineTo(0, 50)
    	shape:closePath()
    	shape:endPath()
     
    	shape:setX(math.random(0, 320 - 100))
    	shape:setY(math.random(0, 480 - 50))
     
    	shape.isFocus = false
     
    	shape:addEventListener(Event.MOUSE_DOWN, onMouseDown, shape)
    	shape:addEventListener(Event.MOUSE_MOVE, onMouseMove, shape)
    	shape:addEventListener(Event.MOUSE_UP, onMouseUp, shape)
     
    	stage:addChild(shape)
    end
     
     
    local info = TextField.new(nil, "drag the shapes around with your mouse or fingers")
    info:setPosition(23, 50)
    stage:addChild(info)

    Likes: Apollo14

    +1 -1 (+1 / -0 ) Share on Facebook
  • olegoleg Member
    edited February 6

    I gave a bad example
    effect" dragging speed" should be done through the ENTER_FRAME or through Movieclip

    Likes: Apollo14

    +1 -1 (+1 / -0 ) Share on Facebook
  • antixantix Member
    edited February 6
    Just to muddy the waters here a little but when you have a game object it is a good practice to use a 2D vector to describe it's position in the game world...
    -- generally a lot of people use this...
    local object.x0, object.y0 = 320, 160
     
    -- this is better
    object.position = (x = 320, y = 160}
     
    -- and to access it...
    local p = object.position
    local x0, y0 = p.x, p.y
     
    x0 -= 4
    y0 += 7
     
    p.x, p.y = x0, y0
    The main reason I bring this up is that when you start to create more complex applications and you need help from the internet.. 99% of places you go will be using 2D vectors. Mashing code from online into your own projects becomes much easier :)

    Likes: Apollo14

    Follow me on FaceBook Check out my DevBlog, my GitHub, and try my games...
    Falling Animals | Breaky Wall | Exetor | Mini Putt Golfing
    +1 -1 (+1 / -0 ) Share on Facebook
  • Apollo14Apollo14 Member
    edited February 12
    Guys, I'm confused about different font methods:
    Perfomance-wise, what is a better practice - to always use TTFont.new or Font.new?
    newFont1= TTFont.new("stylus.ttf", 50, true)
    newFont2= Font.new("stylus.txt", "stylus.png", true)
    newFont3= BMFont.new("stylus.txt", "stylus.png")
    BTW how do we adopt bitmap fonts like this for Gideros?

    There are plenty of them on the web, but their metadata is different from what we need. How to set up their metadata?

    Likes: Apollo14

    +1 -1 (+1 / -0 ) Share on Facebook
  • olegoleg Member
    I cache only the required characters
    local font = TTFont.new("font/ELEPHNT.TTF", 20, "SCORE")

    Likes: Apollo14

    +1 -1 (+1 / -0 ) Share on Facebook
  • If you cache the TTFont, I don't think there is much in it in terms of performance.

    I think you can adopt a bitmap font with some manual work, not quite sure what's involved though.

    Likes: Apollo14

    My Gideros games: www.totebo.com
    +1 -1 (+1 / -0 ) Share on Facebook
  • The bitmap font will require manually editing (or generating) a .fnt file that accompanies the png image of the font.

    You can generate a bitmap font using Gideros Font Maker and check out the fnt file to see how it is formatted.

    Likes: Apollo14

    Follow me on FaceBook Check out my DevBlog, my GitHub, and try my games...
    Falling Animals | Breaky Wall | Exetor | Mini Putt Golfing
    +1 -1 (+1 / -0 ) Share on Facebook
  • When we call
    print(_VERSION)
    it displays "Lua 5.1", while last official Lua version is 5.3.4

    Is implementing Lua 5.3.4 too complicated so maintainers decided to skip it?
    (actually I don't think we're gonna miss anything from 5.3.4, because 5.1 is more than enough for all and everything :)
  • Guys, did I get it right? "Macro Functions" serve the purpose of perfomance only?
    Or they are also convenient for some other purposes?
  • olegoleg Member
    macro functions are constants, they are more economical than variables

    Likes: antix

    +1 -1 (+1 / -0 ) Share on Facebook
  • Apollo14Apollo14 Member
    edited February 20
    Guys, few more questions appeared! :smiley:
    #1 How does Screen:setPosition() work?

    I've tested this line:
    Screen:setPosition(50,50)
    Application just spawns error:
    main.lua:467: index '__userdata' cannot be found
    stack traceback:
    main.lua:467: in main chunk
    #2 And how does application:setKeyboardVisibility work?
    This added line doesn't show anything on my Android device:
    application:setKeyboardVisibility(visible)
    #3 I've seen in 2017.9 release notes:
    Consistent random number generator (Core.random()/Core.randomSeed())
    I've tested it, so what is the difference between this generator and Lua's original math.random?

    This method always assigns the same value to 'randNum':
    math.randomseed(10)
    randNum = math.random(1,10)
    print(randNum)
    While new method assigns different values everytime:
    Core.randomSeed(10)
    randNum = Core.random(1,10)
    print(randNum)
    In what cases new method is more convenient?
Sign In or Register to comment.