仅检测 agk 顶部的精灵碰撞

如何解决仅检测 agk 顶部的精灵碰撞

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

我检查碰撞是否在顶部的方法是使用第 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 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 30,0 ) // 30fps instead of 60 to save battery
SetScissor( 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,block2y)
            SetSpriteColor(count,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,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,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,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,GetSpriteY(block))
        next block
     
    endwhile
    
    
    
    
     
    Sync()
loop







 








解决方法

来自 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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res