zoukankan      html  css  js  c++  java
  • love2d教程3输入和音乐

    love2d的输入包括love.keyboard,love.mouse,love.joystick(手柄)
    键盘:
    按键检测是否按下可以用love.keyboard.isDown("键值"),如love.keyboard.isDown("up"),
    检测向上键是否按下,按下返回真.
    还可以用love.keyboard.setKeyRepeat(delay, interval)设置重复按键的延时和间隔,
    当延时(delay)设为0时,不允许按住一个键不放,interval没明白怎么回事.如果你知道,请告诉我.
    也可以在love.keypressed(key)回调函数中检测是哪个按键(key),我试了一下,不支持按住一个
    键不放.
    鼠标:
    鼠标在love.mouse模块中,函数很多,可以自己查看wiki
    手柄:
    手柄在love.joystick模块中,由于没手柄,没有试验,与键盘,鼠标应该类似.

    love2d的音乐功能在love.audio模块里,底层使用openal,支持20多种音乐格式.
    音乐只有一种数据类型"source".使用love.audio.newSource可以创建一个source对象
    sound = love.audio.newSource("pling.wav", "static") --有static和stream两种模式,分别对应短和长的音乐
    默认是stream模式.
    wiki上两种模式的对比如下,可见对长音乐应该使用strem模式
    Keep in mind that, if you pass love.audio.newSource "static" as a second argument,
    the sound file will be expanded into memory, so if you load a 5MB compressed .ogg file
    that way, it would consume ~50MB RAM when fully decompressed. Consider not using "static"
    in such cases.

    接着可以使用source对象的方法控制音乐的一些效果如:
    sound:setVolume(0.9) -- 音量为90%
    sound:setPitch(0.5) -- 音调为50%,类似频率的高低

    下面是我的示例程序,演示了鼠标轨迹的捕捉和音乐的播放.

    love2d-tutor3

    tutor3.lua

    local mousePos={}
    qx,qy=100,100
    playSound=false
    function drawQuad(x,y,width,height)
        love.graphics.setColor(0, 255, 0)
        love.graphics.quad("fill",x,y,x+width,y,x+width,y+height,x,y+height)
    end
    
    function drawMouseTrack()
        love.graphics.setPointSize( 3 )
        local x, y = love.mouse.getPosition( )
        table.insert(mousePos,x)
        table.insert(mousePos,y)
        love.graphics.setColor(255,0,0)
        --取出mousePos table里的元素,下面即c语言的for(int i=1,i<mousePos.legth;i+=2)
        for i=1,#mousePos,2 do
        love.graphics.point(mousePos[i],mousePos[i+1])
        end
    end

    main.lua

    require("tutor03")
    
    function love.load()
    sound = love.audio.newSource("assets/sound.wav", "static")
    music = love.audio.newSource("assets/music.mp3") --默认是stream,动态加载,适合播放时间长的音乐
    music:setVolume(0.5)
    love.audio.play(music)
    end
    
    function love.draw()
        drawMouseTrack()
        drawQuad(qx,qy,20,20)
        love.graphics.print("qx=" .. qx .. "  qy=" .. qy,20,20)
    
    end
    
    function love.update(dt)
        --按键检测
        if(love.keyboard.isDown("up")) then
        qy=qy-5
        end
        if(love.keyboard.isDown("down")) then
        qy=qy+5
        end
        if(love.keyboard.isDown("left")) then
        qx=qx-5
        end
        if(love.keyboard.isDown("right")) then
        qx=qx+5
        end
    
    
        --检测小方块是否运动到窗口边界
        if(qx>=780)then
        qx=780
        playSound=true
        elseif(qx<0)then
        qx=0
        playSound=true
        end
        if(qy>=580)then
        qy=580
        playSound=true
        elseif(qy<=0)then
        qy=0
        playSound=true
        end
    
    
        if(playSound==true) then
            love.audio.play(sound)
            playSound=false
        end
    
    end
    
    function love.keypressed(key)
    --~     if(key=="up") then
    --~     qy=qy-5
    --~     elseif(key=="down") then
    --~     qy=qy+5
    --~     elseif(key=="left") then
    --~     qx=qx-5
    --~     elseif(key=="right") then
    --~     qx=qx+5
    --~     end
    end

    源码下载http://pan.baidu.com/share/link?shareid=123821&uk=1913510140


    作者:半山
    出处:http://www.cnblogs.com/xdao/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

  • 相关阅读:
    android 中文 api (43) —— Chronometer
    SVN客户端清除密码
    Android 中文 API (35) —— ImageSwitcher
    Android 中文API (46) —— SimpleAdapter
    Android 中文 API (28) —— CheckedTextView
    Android 中文 API (36) —— Toast
    Android 中文 API (29) —— CompoundButton
    android 中文 API (41) —— RatingBar.OnRatingBarChangeListener
    Android 中文 API (30) —— CompoundButton.OnCheckedChangeListener
    Android 中文 API (24) —— MultiAutoCompleteTextView.CommaTokenizer
  • 原文地址:https://www.cnblogs.com/xdao/p/2808280.html
Copyright © 2011-2022 走看看