【问题标题】:Detecting only sprite collisions on top in agk在agk中仅检测顶部的精灵碰撞
【发布时间】:2021-08-29 23:45:42
【问题描述】:

我正在尝试制作一个类似于引爆积木的积木游戏,您可以在其中将积木堆叠在先前积木的顶部以建造一座塔。游戏的基本功能运行良好,但是我遇到了一些碰撞问题。目前,我可以让它检测任何碰撞,但我希望它只检测块是否碰撞/降落在前一个块的顶部而不是侧面。但是它并没有按预期工作。

我检查碰撞是否在顶部的方法是使用第 128 行的 if 语句检查 GetSpriteY(count) + GetSpriteHeight(count)) ”时它可以工作,但会检测到所有碰撞,并且不仅检测到顶部的碰撞。

不知道该怎么做...感谢任何帮助

// Project: tippin
// Created: 2021-06-08

// show all errors
SetErrorMode(2)

// set window properties
SetWindowTitle( "tippin" )
SetWindowSize( 1024, 768, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window

// set display properties
SetVirtualResolution( 1024, 768 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 30, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts




block2x= 450
block2y = 10
//LoadImage(2,"ball.png")



base =1
//make base block
//LoadImage(base,"hoop.png")
CreateSprite(base,0)
SetSpriteSize(base,170,50)
SetSpritePosition(base,400,700)
SetSpriteColor(base,55,232,35,255)




//define variables
startScreen=1
dx1=1
cx1=0.6
dy1=1
x1=1
cy1=0
 
score= 0
gameon= 1
count=2      
needNewSprite= 1

do
   while startScreen=1
        Print("Tippin Blocks")
        Print("Press ENTER to begin")
        Print("Press ESC to quit")
        if GetRawKeyPressed(13)=1 //enter to begin
            startScreen=0
            exit
        else
            if GetRawKeyPressed(27)=1 //end game if esc is pressed
                end
            endif
        endif
        sync()
    //box can move
    SetSpritePhysicsOn(1,2)
    
    
    endwhile
    

i = 0   
Print(score)
    while i < 10 and gameon
        i = i + 1
        //i allows loop to not keep running forever
     
        //to make new sprites
        if needNewSprite
            CreateSprite(count,0)
            SetSpriteSize(count,100,100)
            SetSpritePosition(count,400,block2y)
            SetSpriteColor(count,200,200,220,200)
            SetSpritePhysicsOn(count,3)
            needNewSprite = 0
        endif
     
        //to make the base bounce off walls   
        basex = GetSpriteX(1)
     
        if basex <= 0 //if hits left side then change speed and direction to the right
            dx1 = 1
            basex = 0
            //SetSpritePhysicsVelocity(2,100,0)
        endif
         
        if  basex >= GetVirtualWidth() - GetSpriteWidth(base) //if hits right side then change speed to left side
            dx1 = -1
            basex = GetVirtualWidth() - GetSpriteWidth(base)
            //SetSpritePhysicsVelocity(2,-100,0)
        endif
     
        basex = basex + dx1*cx1
        //makes the base move
        SetSpritePosition(base, basex,GetVirtualHeight() - GetSpriteHeight(base))
     
     
        //if press space button,move the ball down
        if GetRawKeyPressed(32)
            SetSpritePhysicsVelocity(count,0,800)
    
        endif
    
    //check if sprite collides with previous sprite
        if GetSpriteCollision(count,count-1)=1
            //check if it collision is on the top surface
            //not working properly right now, counts every collision
            if count = 2:
                blockx = GetSpriteX(count) + dx1*cx1
                SetSpritePosition(count,blockx,GetSpriteY(count))
                score = score + 1
                count = count + 1
                needNewSprite = 1
                continue
            endif
            
            if (GetSpriteY(count) + GetSpriteHeight(count)) <= GetSpriteY(count - 1)
                blockx = GetSpriteX(count) + dx1*cx1
                SetSpritePosition(count,blockx,GetSpriteY(count))
                score = score + 1
                //get x coordinate of collision and make it go in same direction as base block
                //need new sprite
                count = count + 1
                needNewSprite = 1
                continue //prevents errors since next sprite has not been made
            endif
            
        endif
     
     
        //end if hits bottom
        if GetSpriteY(count) >= GetVirtualHeight() - GetSpriteHeight(count)
            end
            gameon = 0
      
        endif  
        //for loop where to start and till when
        //from the second block to the second last block itll keep updating the location of each
        for block = 2 to count - 1:
            blockx = GetSpriteX(block) + dx1*cx1
            SetSpritePosition(block,blockx,GetSpriteY(block))
        next block
     
    endwhile
    
    
    
    
     
    Sync()
loop







 








【问题讨论】:

    标签: 2d game-engine 2d-games agk-basic


    【解决方案1】:

    来自 AGK 的手册:

    一旦您将精灵设置为由物理控制,您应该谨慎使用 SetSpritePosition 和 SetSpriteAngle,因为它们会中断精灵的物理运动。

    要么使用内部物理系统,要么自己控制一切,将两者混合起来并不是一个好主意,因为看起来你正在尝试这样做。我的猜测是您如何/何时增加“计数”存在问题,并且它没有检查您可能认为应该检查的精灵。

    这里有一个简单的框架可以帮助你。目前我面前没有 AGK,所以我无法测试。我的另一个提示是选择更有意义的变量名。

    最后一件事,作为 AGK 论坛近 20 年的成员,它确实是寻求帮助以解决有关该语言的问题的最佳场所,因为大多数其他地方通常不熟悉它。

    // To track the sprites/blocks on the stack
    Global stackedBlocks as integer[]
    
    
    baseSprite = newBlock()
    
    
    topSprite = baseSprite
    
    
    
    
    DO
    
    
        // Loop through all sprites in the stack.
        // Due to movement of the base platform, blocks could fall off 
        // and thus changing the top most block in the stack. This loop
        // not only determines which block is currently on top but also
        // checks for blocks that may have fallen off the map
        
        y = 9999
        
        for i = 0 to stackedBlocks.length-1
            if stackedBlocks[i] <> fallingSprite
                if getSpriteY(stackedBlocks[i]) < y
                    topSprite = stackedBlocks[i]
                endif
            endif
            
            // Block fell off the map
            if getSpriteY(stackedBlocks[i]) > getVirtualHeight()
                // do something here
            endif
            
        next i
        
        
    
    
        // If the falling block hits the top of the stack
        if getPhysicsCollision(fallingSprite, topSprite) = 1
            
            // The sprite that was falling should now be on the top of the stack
            topSprite = fallingSprite
            
            // This block is now on the stack, add the sprite index to the array
            stackedBlocks.insert(topSprite)
            
            fallingSprite = newBlock()
    
        endif
        
        
    
        
    
    
        sync()
    LOOP
    
    
    
    
    
    // Create a new block ready for dropping
    function newBlock()
        s = createSprite(0)
        setSpriteSize(s, 170, 50)
        setSpritePosition(s, 400, 700)
        setSpriteColor(s, 55, 232, 35, 255)
        
        stackedBlocks.insert(s)
        
    endfunction s
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多