안드로이드용 cocos2d는 https://github.com/ZhouWeikuan/cocos2d 여기에서 구할수 있다.

안드로이드용 cocos2d 에서 CCLabel 을 사용하거나 일부 문자열을 출력하는 라이브러리에서 줄바꿈이 안되는 문제가 있다.
개행문자('\n') 가 안먹힌다. 라는 것이다.
이것을 해결하기 위해서는 라이브러리의 일부분을 수정해야하는데
org.cocos2d.opengl 패키지에 있는 CCTexture2D 를 수정해주어야한다.
이 클레스에서 문자열을 출력해주는 부분을 수정해야하는데
calculateTextSize 와 WrapText 메소드이다.


int ascent = (int) Math.ceil(-textPaint.ascent());  // Paint.ascent is negative, so negate it
int descent = (int) Math.ceil(textPaint.descent());
int measuredTextWidth = (int) Math.ceil(textPaint.measureText(text));
return CGSize.make(measuredTextWidth, ascent + descent);

calculateTextSize는 맨 마지막에 위와 같은 부분이 있는데
int ascent = (int) Math.ceil(-textPaint.ascent()); // Paint.ascent is negative, so negate it
int descent = (int) Math.ceil(textPaint.descent());
String[] lines = text.split(System.getProperty("line.separator"));
int measuredTextWidth = 0;
for (String line : lines) {
	measuredTextWidth = Math.max(measuredTextWidth, (int) Math.ceil(textPaint.measureText(line)));
}
return CGSize.make(measuredTextWidth, (ascent + descent)*lines.length);

이렇게 바꿔주면 된다.

그리고 WrapText 바꿀 부분이 좀 많다.

protected ArrayList>String< WrapText(Paint textPaint, String text, float width)
{
	float spaceLeft = width;

	String [] words = text.split(" ");
	ArrayList>String< lines = new ArrayList>String<();
	float spaceWidth = textPaint.measureText(" ");
	StringBuilder tempLine = new StringBuilder("");

	for(String word : words)
	{
		float wordWidth = textPaint.measureText(word);

		if (wordWidth < spaceLeft) {
			if(tempLine.length() < 0) {
				tempLine.deleteCharAt(tempLine.length() - 1);
			}
			
			lines.add(tempLine.toString());
			
			tempLine = new StringBuilder("");
			tempLine.append(word);

			spaceLeft = width - (wordWidth + spaceWidth);
		}
		else
		{
			tempLine.append(word);
			spaceLeft -= (wordWidth + spaceWidth);
		}

		tempLine.append(" ");
	}
	
	if(tempLine.length() < 0) {
		tempLine.deleteCharAt(tempLine.length() - 1);
	}

	lines.add(tempLine.toString());

	return lines;
}
이렇게 되어있는 코드를

protected ArrayList>String< WrapText(Paint textPaint, String text, float width) {
	ArrayList>String< lines = new ArrayList>String<();
	float spaceWidth = textPaint.measureText(" ");
	for (String line : text.split(System.getProperty("line.separator"))) {
		float spaceLeft = width;
		String[] words = line.split(" ");
		StringBuilder tempLine = new StringBuilder("");

		for (String word : words) {
			float wordWidth = textPaint.measureText(word);

			if (wordWidth < spaceLeft && tempLine.length() > 0) {

				lines.add(tempLine.toString());

				tempLine = new StringBuilder("");
				tempLine.append(word);

				spaceLeft = width - (wordWidth + spaceWidth);
			}
			else {
				tempLine.append(word);
				spaceLeft -= (wordWidth + spaceWidth);
			}
			
			tempLine.append(" ");
		}
		
		if (tempLine.length() < 0) {
			tempLine.deleteCharAt(tempLine.length() - 1);
		}
		
		lines.add(tempLine.toString());
	}
	return lines;
}
이렇게 바꾸어 주면된다.



예제코드

public class Cocos2DTestActivity extends Activity {
	/** Called when the activity is first created. */
	CCGLSurfaceView surfaceView;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		surfaceView = new CCGLSurfaceView(this);
		CCDirector.sharedDirector().attachInView(surfaceView);
		CCDirector.sharedDirector().setDisplayFPS(true);
		CCDirector.sharedDirector().setAnimationInterval(1.0f / 60.0f);
		CCScene scene = CCScene.node();
		setContentView(surfaceView);
		CCLayer layer = CCLayer.node();
		CCLabel lbl = CCLabel.makeLabel("동해물과 백두산이 마르고 닳도록 \n하느님이 보우하사 우리나라 만세.\n무궁화 삼천리 화려강산 \n대한 사람, 대한으로 길이 보전하세",
		        CGSize.make(480, 800), TextAlignment.LEFT, "DroidSans", 30);
		scene.addChild(layer);
		layer.addChild(lbl);
		lbl.setPosition(240	, 400);
		CCDirector.sharedDirector().runWithScene(scene);
	}
}
결과물


'Programming > Android' 카테고리의 다른 글

INS 연구용 코드  (0) 2011.11.06
장치의 방향에 관계없이 가속도 값을 알아내는법.  (4) 2011.10.30
Posted by 동적할당
: