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

Quick questions about Gideros&Lua

2456

Comments

  • Apollo14Apollo14 Member
    edited December 2017
    Hi guys! Few more newbie questions are coming!

    #1: How to prevent screen turning off on my Android, while I'm using Gideros Android Player? It's pretty inconvenient to turn it on again and again while I'm testing app. Do I need to set an exception somewhere inside Android OS?

    #2: How to regulate animation speed?
    I've tried to loop basic animation, but it's too quick by default, I need it slower:
    frame1 = Bitmap.new(Texture.new("w1.png"))
    frame2 = Bitmap.new(Texture.new("w2.png"))
    frame3 = Bitmap.new(Texture.new("w3.png"))
    frame4 = Bitmap.new(Texture.new("w4.png"))
    frame5 = Bitmap.new(Texture.new("w5.png"))
    frame6 = Bitmap.new(Texture.new("w6.png"))
    frame7 = Bitmap.new(Texture.new("w7.png"))
    frame8 = Bitmap.new(Texture.new("w8.png"))
     
    enemyRotate = MovieClip.new{
    	{1, 1, frame1},	
    	{2, 2, frame2},	
    	{3, 3, frame3},	
    	{4, 4, frame4},	
    	{5, 5, frame5},	
    	{6, 6, frame6},
    	{7, 7, frame7},
    	{8, 8, frame8},
    }
     
    enemyRotate:setGotoAction(8,1)
    enemyRotate:setScale(0.58)
    stage:addChild(enemyRotate)

    Likes: Apollo14

    +1 -1 (+1 / -0 ) Share on Facebook
  • @SinisterSoft - lol, I was just looking for the same thing :) Thanks!
  • olegoleg Member
    edited December 2017
  • Thx, guys!
    uh, I found how to make animation slower :D I should've been more alert reading reference manual :)

    Likes: antix

    +1 -1 (+1 / -0 ) Share on Facebook
  • Apollo14Apollo14 Member
    edited December 2017
    Hi guys! How to regulate animation speed on the fly?
    For example when button is clicked, animation starts playing a little bit faster.

    (I don't see in documentation any reference, like "MovieClip:setSpeed" or "MovieClip:increaseSpeed()", etc)
  • olegoleg Member
    edited December 2017
    frame1 = Bitmap.new(Texture.new("w1.png"))
    frame2 = Bitmap.new(Texture.new("w2.png"))
    frame3 = Bitmap.new(Texture.new("w3.png"))
     
     
    enemyRotate = MovieClip.new{
    	{1, 1, frame1},	
    	{2, 2, frame2},	
    	{3, 3, frame3},	
     
            {5, 10, frame1},	
    	{11, 15, frame2},	
    	{16, 20, frame3}	
    }
     
     
     
     
    function slow()
     
             enemyRotate:setGotoAction(20,5)
               enemyRotate:gotoAndPlay (5)
    end
     
     
    function fast()
           enemyRotate:setGotoAction(3,1)
           enemyRotate:gotoAndPlay (1)
    end
     
    fast()

    Likes: antix, Apollo14

    +1 -1 (+2 / -0 ) Share on Facebook
  • Hi guys!
    Why am I getting error here?
    function arguments expected near 'then'
    What's wrong with syntax?
    function MoveOrcGreenX(event)
    	if event.x > orcGreen:getX then
    	print("Mouse.X coordinate is bigger then orcGreen.X")
    	end
    end
    stage:addEventListener(Event.MOUSE_DOWN, MoveOrcGreenOnX)
    syntax-error.png
    523 x 139 - 6K
  • hgy29hgy29 Maintainer
    edited January 17
    Shouldn't it be:
    function MoveOrcGreenX(event)
    	if event.x>orcGreen:getX() then
    	print("Mouse.X coordinate is bigger then orcGreen.X")
    	end
    end

    Likes: Apollo14

    +1 -1 (+1 / -0 ) Share on Facebook
  • keszegh said:

    what is ";" for in this code?

    forum's little bug
    I meant in my post:
    if event.x "is bigger than" orcGreen:getX()
  • olegoleg Member
    this is a protection from SQL injection, replaces dangerous characters

    Likes: Apollo14

    image.png
    556 x 241 - 32K
    +1 -1 (+1 / -0 ) Share on Facebook
  • Guys! Why does my spite shake and how to avoid it? :)
    function MoveOrcGreenX()
    	local touchXloc = touchX
    	if orcGreen:getX() > touchXloc
    	then
    	orcGreen:setX(orcGreen:getX() - 3)
    	elseif orcGreen:getX() < touchXloc then
    	orcGreen:setX(orcGreen:getX() + 3)
    	end
    end
    stage:addEventListener(Event.ENTER_FRAME, MoveOrcGreenX)
    gideros_shake.gif
    479 x 139 - 536K
  • /joke on
    @Apollo14 look at him he is obviously on steroids, totally green so his shaking is normal:D
    \joke off

    If you want to move your sprite better move him with touch event(touch start, touch end) not on enter frame event, i guess after your move finish your orc is changing his place +3 and -3 respectively on every frame, because of it he is shaking.

    if you want to continue to move him on enter frame you can make a check value in your touch end event like moving= false , and use this check variable on your onenter frame event in the logical statements. So you will be sure that your green orc will not move anywhere even on steroids:D

    Likes: Apollo14

    +1 -1 (+1 / -0 ) Share on Facebook
  • olegoleg Member
    edited January 18
    function speed_move(sprite,x2,y2,speed)
            speed=speed/1000*60  --60fps
            x1, y1 = sprite:getPosition()
     
            sprite.move = MovieClip.new{
                            {1, speed, sprite, {x = {x1, x2, "linear"},y = {y1, y2, "linear"}}}  
                    }
            sprite.move:gotoAndPlay(1)
    end
     
    speed_move(orcGreen,touchX,orcGreen:getY(),50)

    Likes: Apollo14

    +1 -1 (+1 / -0 ) Share on Facebook
  • Apollo14Apollo14 Member
    edited January 18
    thx @talis & @oleg !
    Though it's not a proper way to move my steroid orc with ENTER_FRAME, I wonder how to avoid shaking with boolean "isMoving:?
    I introduced boolean but it still shakes :/
    function MoveOrcGreenX()
    	local touchXloc = touchX
    	local isMoving = false
     
    if isMoving == false then
    	if orcGreen:getX() > touchXloc
    	then
    	 isMoving = true
    	 orcGreen:setX(orcGreen:getX() - 3)
    	 orcGreen:setScaleX(-1)
    	 isMoving = false
     
    	elseif orcGreen:getX() < touchXloc then
    	 isMoving = true
    	 orcGreen:setX(orcGreen:getX() + 3)
    	 orcGreen:setScaleX(1)
    	 isMoving = false
    	end
    end
    end --function end
    stage:addEventListener(Event.ENTER_FRAME, MoveOrcGreenX)
  • olegoleg Member
    elseif orcGreen:getX() < touchXloc then
    	 isMoving = true
    if touchXloc-orcGreen:getX()<3  then 
     
     orcGreen:setX(touchXloc)
    else
    	 orcGreen:setX(orcGreen:getX() + 3)
    end
    	 orcGreen:setScaleX(1)
    	 isMoving = false
    	end</pre>

    Likes: Apollo14, antix

    +1 -1 (+2 / -0 ) Share on Facebook
  • I have 4 scenes.
    On mouse clicked if current scene is 1, then go to scene 2
    On mouse clicked if current scene is 2, then go to scene 3
    On mouse clicked if current scene is 3, then go to scene 4
    On mouse clicked if current scene is 4, then go back to scene 1

    Can this code be optimized?
    currentScene = 1
    function onMouseDownFunc()
    	if currentScene ==1 then
    	sceneManager:changeScene("scene2", 1, SceneManager.fade)
    	currentScene = 2
    	elseif currentScene ==2 then
    	sceneManager:changeScene("scene3", 1, SceneManager.fade)
    	currentScene = 3
    	elseif currentScene ==3 then
    	sceneManager:changeScene("scene4", 1, SceneManager.fade)
    	currentScene = 4
    	elseif currentScene ==4 then
    	sceneManager:changeScene("scene1", 1, SceneManager.fade)
    	currentScene = 1
    	end
    end
    stage:addEventListener(Event.MOUSE_DOWN, onMouseDownFunc)
  • hgy29hgy29 Maintainer
    currentScene = 1
    function onMouseDownFunc()
    	local nextScene=(currentScene%4)+1
    	sceneManager:changeScene("scene"..nextScene, 1, SceneManager.fade)
    	currentScene = nextScene
    end
    stage:addEventListener(Event.MOUSE_DOWN, onMouseDownFunc)

    Likes: oleg, Apollo14, antix, pie

    +1 -1 (+4 / -0 ) Share on Facebook
  • @hgy29 nice optimization thx! :smile:

    I'm in the process of studying Scene Manager...
    Why I'm getting an error here? I've read @ar2rsawseen 's post, tried to do somewhat similar :/
    Scene2 = Core.class(Sprite)
     
    function Scene2:init()
    	local sprite2 = Bitmap.new(Texture.new("sprite2.png"))
    	sprite2:setScale(0.7)
    	sprite2:setAnchorPoint(0.5,0.5)
    	sprite2:setPosition(centrX, centrY)
    	self:addChild(sprite2)
     
        self:addEventListener("enterEnd", self.onEnterEnd, self)
    end
     
    function Scene2:onEnterEnd()
        print("scene fully entered")
    end
    scenes/Scene2.lua:10: attempt to index global '    self' (a nil value)
    stack traceback:
    	scenes/Scene2.lua:10: in function 'init'
    	[string "property.lua"]:52: in function '__new'
    	[string "property.lua"]:59: in function 'new'
    	scenemanager.lua:287: in function 'changeScene'
    	main.lua:23: in function
  • hgy29hgy29 Maintainer
    lua should say the variable is 'self', not ' self' (with the extra spaces before the name).
    Maybe your file contains some blank characters (not spaces or tabs) before the self:addEventListener(). It could have happened if you copied/pasted the code from a web page.

    Likes: Apollo14

    +1 -1 (+1 / -0 ) Share on Facebook
  • Apollo14Apollo14 Member
    edited January 24
    hgy29 said:

    lua should say the variable is 'self', not ' self' (with the extra spaces before the name).
    Maybe your file contains some blank characters (not spaces or tabs) before the self:addEventListener(). It could have happened if you copied/pasted the code from a web page.

    Thanks a lot! Indeed there was a blank character somewhere. After rewriting code it works fine!
  • Guys! How to visualize swipe gesture?
    I'm trying to spawn black pixels continuously while swiping, but they spawn only once.

    First I've created class "dot.lua":
    Dot = Core.class(Sprite)
     
    function Dot:init()
    	local blackPixel = Pixel.new(0x000000, 1, 4, 4)
    	self:addChild(blackPixel)
    	--Pixels should disappear after 500ms:
    	local timerDelayed = Timer.delayedCall(500, function()
    	self:removeChild(blackPixel)
    	end)
    end
    Inside "main.lua" I added:
    function onMouseDownFunc(event)
    	touchX, touchY = event.touch.x, event.touch.y
     
    	stage:addEventListener(Event.ENTER_FRAME, function()
    	local locDot = Dot.new()
    	locDot:setPosition(touchX, touchY)
    	stage:addChild(locDot)
    	end)
     
    end
     
    function onTouchesEndFunc(event)
    	--
    end
     
    stage:addEventListener(Event.TOUCHES_BEGIN, onMouseDownFunc)
    stage:addEventListener(Event.TOUCHES_END, onTouchesEndFunc)
    trying_to_visualize_swipe.gif
    479 x 139 - 82K
  • Apollo14Apollo14 Member
    edited January 27
    thx @oleg
    I've tried Event.TOUCHES_MOVE, now effect became better, but there are lots of interruptions on the line: http://giderosmobile.com/forum/uploads/editor/8j/mg4jef8se8g1.gif


    I've found @atilim 's "Fruit Ninja" old prototype on forum: http://giderosmobile.com/forum/discussion/1214/fruit-ninja-template-who-wants-to-contribute-with-me/p1

    He did it using shape. Does somebody know if there is a less complicated way to do it?
    local shape = Shape.new()
    stage:addChild(shape)
     
    local x, y
     
    local xs = {}
    local ys = {}
    local ss = {}
     
    function onMouseDown(event)
    	x,y = event.x,event.y
    end
     
    function onMouseMove(event)
    	x,y = event.x,event.y
    end
     
    function onMouseUp()
    	x,y = nil,nil
    end
     
    function onEnterFrame()
    	if x and y then
    		xs[#xs + 1] = x
    		ys[#ys + 1] = y
    		ss[#ss + 1] = 10
    	end
     
    	while #ss > 0 and ss[1] <= 1 do
    		table.remove(xs, 1)
    		table.remove(ys, 1)
    		table.remove(ss, 1)
    	end
     
    	shape:clear()
    	for i=1,#xs-1 do
    		shape:setLineStyle(ss[i], 0x8080ff)
    		shape:beginPath()
    		shape:moveTo(xs[i], ys[i])
    		shape:lineTo(xs[i+1], ys[i+1])
    		shape:endPath()
    	end
     
    	for i=1,#ss do
    		ss[i] = ss[i] - 1
    	end
     
    	if #ss > 0 then
    		local x2 = xs[#ss]
    		local y2 = ys[#ss]
     
    		for i=#ss-1, 1, -1 do
    			local x1 = xs[i]
    			local y1 = ys[i]
     
    			local d = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)
    			if d > 10 then
    				split(x1, y1, x2, y2)
    			end			
    		end
    	end
    end
    swipe_gesture_test.gif
    469 x 139 - 50K
  • olegoleg Member
    edited January 27
    Apollo14 said:

    thx @oleg
    I've tried Event.TOUCHES_MOVE, now effect became better, but there are lots of interruptions on the line:

    variant1:
    save the coordinates of the previous point
    find the distance between the points math.sqrt((X1-X2)^2+(Y1-Y2)^2)
    generate additional points between them
    variant2:
    points to connect lines

    variant3:
    see the screenshot





    11.JPG
    446 x 327 - 17K
    11.JPG 16.7K
    +1 -1 (+3 / -0 ) Share on Facebook
  • antixantix Member
    @oleg has the right suggestion I think :)
    Follow me on FaceBook Check out my DevBlog, my GitHub, and try my games...
    Falling Animals | Breaky Wall | Exetor | Mini Putt Golfing | Ninja Fruit Master
  • Apollo14Apollo14 Member
    edited January 31
    I see that macro constants are available since 2017.10: http://docs.giderosmobile.com/reference/enhancement/macroconstants

    Is it a good practice to use them whenever we can? How do they affect perfomance?
  • They're FAST. I use them whenever I can.
    My Gideros games: www.totebo.com
    +1 -1 (+2 / -0 ) Share on Facebook
  • They improve performance by not having to look up the variable in a table and also because the byte compiler will pre-calculate formulas if it can so constants will end up being part of that precalculation but variable won't.

    eg
    local pi=3.142
    a=pi*2

    will compile into that in bytecode as it can't reduce it because pi is a variable and could change.

    but
    @pi=3.142
    a=pi*2

    will compile to this in bytecode
    a=6.284

    because the compiler will first make it a=3.142*2 then further reduce this to 6.284 before making the bytecode.

    Likes: Apollo14

    +1 -1 (+1 / -0 ) Share on Facebook
Sign In or Register to comment.