代码之家  ›  专栏  ›  技术社区  ›  iPhoneProcessor

Cocos2d 2.0工作代码在Cocos2d 2.2中不工作

  •  1
  • iPhoneProcessor  · 技术社区  · 5 年前

    -(CCSprite *)stripedSpriteWithColor1:(ccColor4F)c1 color2:(ccColor4F)c2 textureWidth:(float)textureWidth
    textureHeight:(float)textureHeight stripes:(int)nStripes {
    
        // 1: Create new CCRenderTexture
        CCRenderTexture *rt = [CCRenderTexture renderTextureWithWidth:textureWidth height:textureHeight];
    
        // 2: Call CCRenderTexture:begin
        [rt beginWithClear:c1.r g:c1.g b:c1.b a:c1.a];
    
        // 3: Draw into the texture
    
        // Layer 1: Stripes
        CGPoint vertices[nStripes*6];
        ccColor4F colors[nStripes*6];
    
        int nVertices = 0;
        float x1 = -textureHeight;
        float x2;
        float y1 = textureHeight;
        float y2 = 0;
        float dx = textureWidth / nStripes * 2;
        float stripeWidth = dx/2;
        for (int i=0; i<nStripes; i++) {
            x2 = x1 + textureHeight;
    
            vertices[nVertices] = CGPointMake(x1, y1);
            colors[nVertices++] = (ccColor4F){c2.r, c2.g, c2.b, c2.a};
    
            vertices[nVertices] = CGPointMake(x1+stripeWidth, y1);
            colors[nVertices++] = (ccColor4F){c2.r, c2.g, c2.b, c2.a};
    
            vertices[nVertices] = CGPointMake(x2, y2);
            colors[nVertices++] = (ccColor4F){c2.r, c2.g, c2.b, c2.a};
    
            vertices[nVertices] = vertices[nVertices-2];
            colors[nVertices++] = (ccColor4F){c2.r, c2.g, c2.b, c2.a};
    
            vertices[nVertices] = vertices[nVertices-2];
            colors[nVertices++] = (ccColor4F){c2.r, c2.g, c2.b, c2.a};
    
            vertices[nVertices] = CGPointMake(x2+stripeWidth, y2);
            colors[nVertices++] = (ccColor4F){c2.r, c2.g, c2.b, c2.a};
            x1 += dx;
        }
    
        self.shaderProgram =
        [[CCShaderCache sharedShaderCache] programForKey:kCCShader_PositionColor];
    
        CCSprite *noise;
        noise = [CCSprite spriteWithFile:@"FarmBase.png"];
        [noise setBlendFunc:(ccBlendFunc){GL_DST_COLOR, GL_ZERO}];
        noise.position = ccp(textureWidth/2, textureHeight/2);
        [noise visit];
        [rt end];
    
        // 5: Create a new Sprite from the texture
        return [CCSprite spriteWithTexture:rt.sprite.texture];
    }
    

    更新: Shader Program not working in Cocos2d 2.2 Obj.C iOS 12

    0 回复  |  直到 5 年前