简体   繁体   English

iPhone-如何制作一个简单的精灵?

[英]iphone-How to make a simple sprite?

I am new to iphone programming. 我是iPhone编程的新手。 I have two uiimageviews that have collisions. 我有两个uiimageviews有冲突。 However, the images inside of them are not squares, so if one hits a corner, it detects a collision that doesnt exist between the two images. 但是,其中的图像不是正方形,因此如果撞到角落,就会检测到两个图像之间不存在碰撞。 This is because UIImageViews are rectangles. 这是因为UIImageViews是矩形。 Since I couldnt find a way to make no collision be detected if it hits a corner (a suggestion for this, if one exists, would be nice), I believe I should make two sprites instead of uiimageviews. 由于我无法找到一种方法来检测碰撞到拐角处的碰撞(对此的建议(如果存在的话,将是一个很好的建议)),我相信我应该制作两个精灵而不是uiimageviews。 I do not have Cocoa 2sd. 我没有可可2SD。 I just have xcode 3.1. 我只有xcode 3.1。 How do I declare a simple sprite and check for collision detectors between them? 如何声明一个简单的精灵,并检查它们之间的碰撞检测器?

The collisions on non transparent pixels would be inefficient using CoreGraphics. 使用CoreGraphics,非透明像素上的碰撞效率会很低。

You are better off investing in learning cocos2d and box 2d. 您最好投资学习cocos2d和box 2d。

You would use beizer paths that you create around the sprite using a tool eg physics editor 您可以使用通过物理编辑器等工具在精灵周围创建的beizer路径

These are then stored in a shapes file which holds the shapes of the sprites. 然后将它们存储在保存小精灵形状的shapes文件中。 They are then connected to sprites in cocos2d and collisions are done on the shapes that are in the background. 然后将它们连接到cocos2d中的精灵,并在背景中的形状上进行碰撞。 You can turn on debug mode to see the shapes on screen. 您可以打开调试模式以查看屏幕上的形状。

This can be taken to the next level by assigning bit masks to the shapes and these on/off switches allow for eg choosing what happens when certain sprites collide with others (maybe trigger a different animation and sound effect for different shape collision pairs). 通过为形状分配位掩码,可以将其带入一个新的水平,并且这些开/关开关允许例如选择当某些精灵与其他精灵碰撞时会发生什么(可能为不同的形状碰撞对触发不同的动画和声音效果)。 One thing to not is it is important to try and box off your sprite shapes with beizer paths that have the least amount of vertices as the more there are the more checks are needed during runtime and your frame rate can drop. 要注意的一件事是,重要的是尝试使用顶点数量最少的beizer路径来取消子图形的形状,因为在运行时需要检查的次数越多,帧速率就会下降。

In short, no it is not suitable to check for collisions on non transparent parts, even in cocos2d as each pixel is needed to be checked. 简而言之,不适合检查非透明零件上的碰撞,即使在cocos2d中也是如此,因为需要检查每个像素。

use this method 使用这种方法

//For img_view1 rect //parameters are x,y,width,height //对于img_view1 rect //参数是x,y,width,height

CGRect image_rect1 = CGRectMake(img_view1.position.x,img_view1.position.y,100,100);

//For img_view2 rect
//parameters are x,y,width,height

CGRect image_rect2 = CGRectMake(img_view2.position.x,img_view2.position.y,100,100)

Collision detection:

    if(CGRectIntersectsRect(image_rect1, image_rect2))
    {
        NSLog(@"Collision Example");
    }

however if you dont want to use CGRect for the views as you can simply use their respective frame properties. 但是,如果您不想对视图使用CGRect,则只需使用它们各自的框架属性即可。

-(BOOL)viewsDoCollide:(UIView *)view1 :(UIView *)view2{
    if(CGRectIntersectsRect(view1.frame, view2.frame))
    {
        return YES;
    }
    return NO;
}

but it would be great if you using cocoa2d 但是如果您使用cocoa2d那就太好了

Please Try this code 请尝试此代码

#define imageWidth 40
#define imageHeight 60

UIImage *image1 = [UIImage imageNamed:@"firstimage.png"];
UIImage *image2 = [UIImage imageNamed: @"secondimage.png"];

CGSize itemSize = CGSizeMake(imageWidth, imageHeight);

UIGraphicsBeginImageContext(itemSize);

CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
[image1 drawInRect:imageRect];
[image2 drawInRect:imageRect];

UIImage *overlappedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

The UIImage overlappedImage is a new image which contains the initial ones, overlapped. UIImage overlaypedImage是一个新图像,其中包含重叠的初始图像。 To be honest, I don't know if this is the best method to achieve this result, but I know it definitely works. 老实说,我不知道这是否是实现此结果的最佳方法,但我知道它肯定有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM