我正在尝试制作一个积木游戏,就像翻倒积木一样,您可以将积木堆叠在先前积木的顶部以建造一座塔。游戏的基本功能运行良好,但是我遇到了一些碰撞问题。目前,我可以让它检测任何碰撞,但我希望它只检测块是否碰撞/落在前一个块的顶部而不是侧面。但是它没有按预期工作。
我检查碰撞是否在顶部的方法是使用第 128 行的 if 语句检查 GetSpriteY(count) + GetSpriteHeight(count)) <= GetSpriteY(count - 1),理论上这将检查左上角下降精灵,添加精灵的高度,看看它是否小于前一个精灵的 y 坐标(意味着当前精灵碰撞在前一个精灵的顶部)。但是,它不起作用,精灵只是不断地相互摔倒。当我将“<”切换到“>”但检测所有碰撞并且不只检测顶部的碰撞时它起作用。
不知道该怎么做...感谢您的帮助
// 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
答案 0 :(得分:0)
来自 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