Quick Links: Download Gideros Studio | Gideros Documentation | Ultimate Guide | Gideros Development Center
Quick questions about Gideros&Lua - Gideros Forum

Quick questions about Gideros&Lua

Apollo14Apollo14 Member
edited November 18 in General questions
Hi guys!

I'm in the process of learning Gideros&Lua...
I thought it would be better to create one topic for quick newbie questions instead of many.

#1 How to create starry sky?
I've tried this code but I'm getting error "main.lua:24: attempt to index global 'star' (a nil value)"
application:setBackgroundColor(0x02020f)
 
--creating one more star every 1000ms
function createStar()
star = Shape.new()
star:setFillStyle(Shape.SOLID, 0xF0F1F2, 1)
star:beginPath()
star:moveTo(0,0)
star:lineTo(1, 0)
star:lineTo(1, 1)
star:lineTo(0, 1)
star:lineTo(0, 0)
star:endPath()
star:setPosition(math.random(20,300), 30)
stage:addChild(star)
end
 
starsTimer = Timer.new(1000, 0)
starsTimer:addEventListener(Event.TIMER,createStar)
starsTimer:start()
 
--moving all stars down
function everyTick()
star:setY(star:getY()+1)
end
 
stage:addEventListener(Event.ENTER_FRAME,everyTick)


Thanks!
«1

Comments

  • Ninjadoodle Member
    edited November 18
    @Apollo14 - instead of using a function for the star, I would create a class. Then every time you create an instance, you can move them individually inside the class it self.

    The way you’re doing it - I think it might be looking for a star before it’s even created.

    Aye: Apollo14

    +1 -1 (+1 / -0 )
  • jdbcjdbc Member
    edited November 18
    --moving all stars down
    function everyTick()
    if (star~=nil) then
    star:setY(star:getY()+1)
    end
    end


    You have not created star the first time your ENTERFRAME listener is executed.

    +1 -1 (+2 / -0 )
  • Apollo14Apollo14 Member
    edited November 18
    if (star~=nil) helps, thx!
    but stars stop moving when other stars spawn, how to make them continue going down?
    I didn't learn about Lua classes yet))
    +1 -1
  • jdbcjdbc Member
    edited November 18
    Apollo14 said:

    if (star~=nil) helps, thx!
    but stars stop moving when other stars spawn, how to make them continue going down?
    I didn't learn about Lua classes yet))

    You have to create a local stars array:
    https://www.lua.org/pil/19.2.html
    stars = {}
    function createStar()
    local star = Shape.new()
    star:setFillStyle(Shape.SOLID, 0xF0F1F2, 1)
    star:beginPath()
    star:moveTo(0,0)
    star:lineTo(1, 0)
    star:lineTo(1, 1)
    star:lineTo(0, 1)
    star:lineTo(0, 0)
    star:endPath()
    star:setPosition(math.random(20,300), 30)
    stage:addChild(star)
    table.add(stars, star)
    end
     
    --moving all stars down
    function everyTick()
    for key,value in pairs(stars) do
    value:setY(value:getY()+1)
    end
    end


    Anyway you have to learn more about Lua language first.
    http://lua-users.org/wiki/ForTutorial

    Aye: hgy29, Apollo14

    +1 -1 (+2 / -0 )
  • @Apollo14 : I think there should me a moving star field particles demo in the examples folder. Particles will be much more efficient.

    Aye: antix, Apollo14

    +1 -1 (+2 / -0 )
  • Ninjadoodle Member
    edited November 18
    Dude, if you create a class for the class like I suggested then they will all move, as you will be controlling them individually inside the class .

    I’ll post you an example shortly :)

    Aye: antix, Apollo14

    +1 -1 (+2 / -0 )
  • antixantix Member
    edited November 18
    @Ninjadoodle, sorry dude bet you to it, bit please do post your implementation too. Everybody can learn from everything :bz

    @Apollo14, making a large array of one pixel shapes wouldn't be the most efficient use of resources. The suggestion of @SinisterSoft to use particles is most likely the best solution. If you really needed separate objects then the Pixel class would be a better option as they would perform faster than Shapes.

    As an exercise I made a small class that creates that moves (in parallax) a multi layered star field made from Pixels. Each layer has more stars than the previous layer and also moves slower than the previous layer also. Maybe you can find a use for it or maybe you will use particles instead :)
    StarField = Core.class(Sprite)
     
    --[[
    numLayers - how many layers there will be
    numStars - the number of stars in the topmost layer
    direction - the direction (0 - 360 degrees) that the layers will move in
    speed - the speed the stars will move at
    color - the RGB color of the stars
    --]]

     
    function StarField:init(numLayers, numStars, direction, speed, color)
    local floor, random = math.floor, math.random
     
    self.speed = speed
    self.direction = direction
     
    local width = application:getContentWidth() -- determine screen bounds
    local height = application:getContentHeight()
    self.width, self.height = width, height
     
    -- create layers of stars
    local layers = {}
    for i = 1, numLayers do -- how many layers we will create
    local layer = {} -- new blank layer
    for j = 1, numStars do -- number of stars to create in current layer
    local pixel = Pixel.new(color, 1, 1, 1) -- a star is born
    pixel:setPosition(random(0, width), random(0, height)) -- set random position
    layer[#layer + 1] = pixel -- add star to new layer
    end
    layers[#layers + 1] =layer -- save layer
    color = color * 0.5 -- layers further away are half as bright as the previous layer
    numStars = floor(numStars * 1.75) -- layers further away have more stars than previous layers
    end
    self.layers = layers
     
    -- add stars in reverse order (darkest to lightest)
    for i = #layers, 1, -1 do
    local layer = layers[i]
    for j = 1, #layer do
    self:addChild(layer[j])
    end
    end
     
    self:setDirection(direction)
    end
     
    -- set new direction for star movement
    function StarField:setDirection(direction)
    local sin, cos = math.sin, math.cos
    local speed = self.speed
    local velocity = { -- create new velocity vector from direction and speed
    x = speed * cos(^<(direction)),
    y = speed * sin(^<(direction)),
    }
    self.velocity = velocity
    self.direction = direction
    end
     
    -- get the direction the stars are currently moving in
    function StarField:getDirection()
    return self.direction
    end
     
    -- set new speed for star movement
    function StarField:setSpeed(speed)
    local sin, cos = math.sin, math.cos
    local direction = self.direction
    local velocity = { -- create new velocity vector from direction and speed
    x = speed * cos(^<(direction)),
    y = speed * sin(^<(direction)),
    }
    self.velocity = velocity
    self.speed = speed
    end
     
    -- get the speed the stars are currently moving at
    function StarField:getSpeed()
    return self.speed
    end
     
    -- call every frame to move stars
    function StarField:update(dt)
    local width, height = self.width, self.height -- screen bounds
     
    local velocity = self.velocity
    local vx, vy = velocity.x * dt, velocity.y * dt -- determine actual speed of movement
     
    local layers = self.layers
    for i = 1, #layers do
    local layer = layers[i] -- next layer to process
    for j = 1, #layer do
    local pixel = layer[j] -- next star to move
     
    local x = pixel:getX() + vx -- update x position
    if x < 0 then
    x += width -- if went off left then reset at right
    elseif x > width then
    x -= width -- if went off right then reset at left
    end
     
    local y = pixel:getY() + vy -- update y position
    if y < 0 then
    y += height -- if went off bottom then reset at top
    elseif y > height then
    y -= height -- if went of top then reset bottom
    end
     
    pixel:setPosition(x, y) -- set the stars new position
    end
     
    vx = vx * 0.5 -- each layer moves at half the speed of the previous layer
    vy = vy * 0.5
    end
    end

    Example of use
    application:setFps(60)
    application:setBackgroundColor(0x000000)
     
    math.randomseed(os.time())
    for i = 1, 4 do -- get rid of any pattern at the start
    math.random()
    end
     
    local stars = StarField.new(3, 60, 45, 57, 0xe0e0e0) -- numLayers, numStars, direction, speed, color
    stage:addChild(stars)
     
    local function onEnterFrame(e)
    local dt = e.deltaTime
     
    stars:update(dt) -- move all stars in all layers
     
    end
     
    stage:addEventListener(Event.ENTER_FRAME, onEnterFrame)


    Aye: Apollo14

    +1 -1 (+1 / -0 )
  • jdbcjdbc Member
    edited November 18
    Use particles demo better, it depends what you want do develop.

    I always use one main.lua to initiates all scenes in SceneManager when the game starts and then Core.class(Sprite) to create classes for everything as you can see in my complete example game:

    https://github.com/jdbcdev/Dots

    Practice with Gideros examples and read some lua tutorial to understand syntax before to start creating a game.

    Aye: Apollo14

    +1 -1 (+1 / -0 )
  • Apollo14Apollo14 Member
    edited November 22
    Admob changed policy:
    https://support.google.com/admob/answer/7562314?ctx=email
    Starting January 23, 2018, we will no longer support Google Mobile Ads (GMA) SDK versions lower than 7.0.0 for Android or iOS. To continue serving mobile ads from AdMob, Ad Exchange, or DFP after this date, please upgrade to the latest Google Mobile Ads SDK:
    What SDK versions does Gideros use?
    +1 -1
  • olegoleg Member
    Apollo14 said:


    What SDK versions does Gideros use?

    Aye: Apollo14

    +1 -1 (+1 / -0 )
  • You can leave those sections blank and it will use the best version found on the computer.

    Aye: Apollo14

    +1 -1 (+1 / -0 )
  • I'm going through "Gideros Mobile Game Development" ebook https://www.packtpub.com/mapt/book/game_development/9781849696708

    It freaks me out, I don't understand a damn f-ing thing in chapter 2.2 "Creating Scenes", either it is extremely newbie-unfriendly, or something is wrong with me personally.

    I'm gonna check what's in "step-by-step tutorials" on forum, if there is something that is possible for me to understand from the start. If there is no such stuff, I don't know what to do. Chapter 2.2 raped my brains and it hurts.
    +1 -1
  • The book was written by a Gideros main dev and forum user: @ar2rsawseen

    Maybe he can help address what you don't understand?

    Aye: Apollo14

    +1 -1 (+1 / -0 )
  • @Apollo14 Yeah I found some things in there a bit confusing, like you :(

    Have you come across this?? http://appcodingeasy.com/Gideros-Mobile/Manage-Scenes-in-Gideros-Mobile

    I think you would learn more from downloading SceneManager from its GitHub page and looking at the example which shows pretty clearly how scenes work. https://github.com/gideros/Scene-Manager

    Let us know if you're still having brain freeze :bz

    Aye: Apollo14

    +1 -1 (+1 / -0 )
  • Ninjadoodle Member
    edited November 28
    Hi @Apollo14

    Scenes is Gideros are actually REALLY easy (it just take a little while to understand).

    Remember to include the scenemanager.lua file, then do something like this ...

    in your main.lua
    sceneManager = SceneManager.new({
     
    ["menu"] = Menu,
    ["stage01"] = Stage01,
    ["stage02"] = Stage02,
    ["stage03"] = Stage03
     
    })
    stage:addChild(sceneManager)
    sceneManager:changeScene("menu")


    in your menu file ...
     
    Menu = gideros.class(Sprite)
     
    function Menu:init()
     
    -- ADD BUTTON TO GO TO LEVELS ETC.
     
    -- FIRST CREATE YOUR BUTTON THEN DO SOMETHING LIKE THIS ...
    local onTouch = function(target, event)
     
    if target:hitTestPoint(event.touch.x, event.touch.y) then
    sceneManager:changeScene("stage01", 1, SceneManager.fade)
    end
    end
     
    stage01Button:addEventListener(Event.TOUCHES_BEGIN, onTouch, stage01Button)
    end


    in your level/stage files ...
    Stage01 = gideros.class(Sprite)
     
    function Stage01:init()
     
    -- DO LEVEL STUFF
     
    end

    Aye: Apollo14

    +1 -1 (+1 / -0 )
  • keszegh Member
    edited November 28
    there are in/out events when changing scene, it's natural to listen to them and stop/start the enterframe event of the current stage on them, e.g.:
    Stage01 = gideros.class(Sprite)
     
    function Stage01:init()
    -- INIT STUFF
    self:addEventListener("enterEnd", self.onTransitionInEnd, self)
    self:addEventListener("exitBegin", self.onTransitionOutBegin, self)
    end
     
    function Stage01:onEnterFrame()
    --DO STUFF REPEATEDLY
    end
     
    function Stage01:onTransitionInEnd()
    self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame,self)
    end
     
    function Stage01:onTransitionOutBegin()
    self:removeEventListener(Event.ENTER_FRAME, self.onEnterFrame,self)
    end

    +1 -1 (+2 / -0 )
  • I add the enter frame event when the scene is initialised - that way any animation I do in the enter frame is there whilst it's being brought in.

    Aye: Apollo14, antix

    +1 -1 (+2 / -0 )
  • @SinisterSoft is right, probably looks better to add enterframe on enterEnd, however adding touch etc. eventlisteners is safer to do on enterBegin.
    +1 -1 (+1 / -0 )
  • Thanks, guys!! I was (and still am) confused with events & event listeners. I need to be patient and keep on learning piece by piece.
    +1 -1
  • Ninjadoodle Member
    edited November 28
    Hi @Apollo14

    Once you understand it, it will become second nature ... Gideros is the easiest/most straight forward tool to code in, that I've encountered (that includes visual tools like Construct 2).

    An Event Listener is just a piece of code you attach to a sprite/object, that waits for something to happen.

    For example a TOUCH EVENT LISTENER will wait for you to touch the sprite you've attached it to, then run the function you've attached to the listener (for example - play a sound effect and goto Scene 2).

    Aye: Apollo14

    +1 -1 (+1 / -0 )
  • Apollo14Apollo14 Member
    edited December 2
    Hi guys!
    I've tried to implement vibration real quick with "application:vibrate()"
    It vibrates too long. Is it possible to make it shorter? Like short standard Android vibe when we navigate apps.
    I didn't find any documentation on this essential feature.

    If vibration settings currently are not supported in Gideros, maybe it would be better to make this default vibration short? It will be very handy, because that long vibration rarely is suitable.
    Thanks!
    +1 -1
  • I think it should be application:vibrate([ms]) -- time in ms

    Aye: Apollo14

    +1 -1 (+1 / -0 )
  • ios doesn't seem to pass it along though...
    void vibrate(int ms)
    {
    #if TARGET_OS_OSX
    #else
    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
    #endif
    }


    But Android does...
    void vibrate(int ms)
    {
    JNIEnv *env = g_getJNIEnv();
     
    jclass localRefCls = env->FindClass("com/giderosmobile/android/player/GiderosApplication");
    jmethodID vibrateID = env->GetStaticMethodID(localRefCls, "vibrate", "(I)V");
    env->CallStaticVoidMethod(localRefCls, vibrateID, (jint)ms);
    env->DeleteLocalRef(localRefCls);
    }
    +1 -1
  • SinisterSoftSinisterSoft Maintainer
    edited December 2
    It seems Apple reject if you use vibrate for anything other than their standard time...
    https://stackoverflow.com/questions/10570553/how-to-set-iphone-vibrate-length

    Only in iOS 10+ is it allowed - using this api...
    https://www.hackingwithswift.com/example-code/uikit/how-to-generate-haptic-feedback-with-uifeedbackgenerator

    +1 -1
  • controller:vibrate(ms) also works for some controllers.
    +1 -1
  • "application:vibrate(50)" is what I wanted! Thanks!! :x
    +1 -1 (+1 / -0 )
  • Don't forget this won't apply to iOS, just Android.

    Aye: Apollo14, antix

    +1 -1 (+2 / -0 )
  • Are there any tutorials or documentation on Particles?
    I checked basic example StarField.gproj, I don't understand how to make starry sky with particles. Can somebody pls make a quick example?
    w=application:getContentWidth()
    h=application:getContentHeight()
    application:setBackgroundColor(0)
     
    stars=Particles.new()
    stars:setPosition(w/2,h/2)
    stage:addChild(stars)
     
    function gameLoop(e)
    stars:addParticles({{x=0,y=0,size=5,ttl=200,speedX=math.random()-0.5,speedY=math.random()-0.5,decay=1.04}})
    end
     
    stage:addEventListener(Event.ENTER_FRAME,gameLoop)
    +1 -1
  • antixantix Member
    @Apollo14 are you trying to display a static star field? Or a moving one? Is it supposed to be 2d? or fake 3d?
    +1 -1
Sign In or Register to comment.