<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>QcMat</title>
	<atom:link href="http://www.qcmat.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.qcmat.com</link>
	<description>Just another WordPress site</description>
	<lastBuildDate>Tue, 07 Dec 2010 14:22:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>Dragging multiple sprites in Cocos2d</title>
		<link>http://www.qcmat.com/dragging-multiple-sprites-in-cocos2d/</link>
		<comments>http://www.qcmat.com/dragging-multiple-sprites-in-cocos2d/#comments</comments>
		<pubDate>Mon, 06 Sep 2010 18:14:55 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[cocos2d]]></category>
		<category><![CDATA[Game development]]></category>
		<category><![CDATA[drag]]></category>
		<category><![CDATA[multitouch]]></category>
		<category><![CDATA[sprite]]></category>

		<guid isPermaLink="false">http://www.qcmat.com/?p=146</guid>
		<description><![CDATA[One of the questions that pops up often when talking to people new at using cocos2d is how to drag multiple sprites. There are lots of ways to achieve this, some more messy code-wise than others. This article will show you how to delegate touches to the sprites themselves, and not have to worry about [...]]]></description>
			<content:encoded><![CDATA[<p><img src="/qcblog/images/multipledrag.jpg" /></p>
<p>One of the questions that pops up often when talking to people new at using cocos2d is how to drag multiple sprites. There are lots of ways to achieve this, some more messy code-wise than others. This article will show you how to delegate touches to the sprites themselves, and not have to worry about it in the layer.</p>
<p><a href="/downloads/DragMultipleSprites.zip">Click here</a> to download the complete project.</p>
<p>This simple project consists of 2 classes :<br />
&#8220;<strong>HelloWorld</strong>&#8221; : The <em>CCLayer</em> where we will have our sprites, plus a button to add more of them<br />
&#8220;<strong>DragSprite</strong>&#8221; : Our <em>CCSprite</em> subclass that will handle touches itself</p>
<p>Let&#8217;s start with the init of the <em>DragSprite</em> class:</p>
<pre class="php">
-<span class="phpOperator">(</span>id<span class="phpOperator">)</span> init
<span class="phpOperator">{</span>
<span class="phpKeyword">	if<span class="phpOperator">(</span></span> <span class="phpOperator">(</span>self=<span class="phpOperator">[</span>super init<span class="phpOperator">]</span> <span class="phpOperator">)</span><span class="phpOperator">)</span> <span class="phpOperator">{</span>
		<span class="phpComment">// Button to add more sprites to the layer
</span>		CCMenuItemFont *add <span class="phpOperator">=</span> <span class="phpOperator">[</span>CCMenuItemFont itemFromString<span class="phpOperator">:</span>@<span class="phpString">"Add Sprite"</span> target<span class="phpOperator">:</span>self selector<span class="phpOperator">:</span>@selector<span class="phpOperator">(</span>addSprite<span class="phpOperator">:</span><span class="phpOperator">)</span><span class="phpOperator">]</span><span class="phpText">;</span>
		add<span class="phpOperator">.</span>position<span class="phpOperator">=</span>ccp<span class="phpOperator">(</span>80,30<span class="phpOperator">)</span><span class="phpText">;</span>
		CCMenu *menu <span class="phpOperator">=</span> <span class="phpOperator">[</span>CCMenu menuWithItems<span class="phpOperator">:</span>add,nil<span class="phpOperator">]</span><span class="phpText">;</span>
		<span class="phpOperator">[</span>self addChild<span class="phpOperator">:</span>menu<span class="phpOperator">]</span><span class="phpText">;</span>
		menu<span class="phpOperator">.</span>position<span class="phpOperator">=</span>ccp<span class="phpOperator">(</span><span class="phpNumber">0</span>,0<span class="phpOperator">)</span><span class="phpText">;</span>
	<span class="phpOperator">}</span>
<span class="phpKeyword">	return </span>self<span class="phpText">;</span>
<span class="phpOperator">}</span>
</pre>
<p><br/></p>
<p>The first thing we do is simple. We create a <em>CCMenuItemFont</em>, a button to add more sprites to the layer. As you can see, when pressed, the button will call a selector called <em>addSprite</em>. Here is the code for this selector :</p>
<pre class="php">
-<span class="phpOperator">(</span>void<span class="phpOperator">)</span> addSprite<span class="phpOperator">:</span><span class="phpOperator">(</span>id<span class="phpOperator">)</span>sender<span class="phpOperator">{</span>
	<span class="phpComment">//
</span>	<span class="phpComment">//	Add a sprite and position it randomly on the screen
</span>	<span class="phpComment">//
</span>	DragSprite *s <span class="phpOperator">=</span> <span class="phpOperator">[</span>DragSprite spriteWithFile<span class="phpOperator">:</span>@<span class="phpString">"smile.png"</span><span class="phpOperator">]</span><span class="phpText">;</span>
	s<span class="phpOperator">.</span>position<span class="phpOperator">=</span>ccp<span class="phpOperator">(</span>arc4random<span class="phpOperator">(</span><span class="phpOperator">)</span> % 480,arc4random<span class="phpOperator">(</span><span class="phpOperator">)</span> % 320<span class="phpOperator">)</span><span class="phpText">;</span>
	<span class="phpOperator">[</span>self addChild<span class="phpOperator">:</span>s<span class="phpOperator">]</span><span class="phpText">;</span>
<span class="phpOperator">}</span>
</pre>
<p><br/></p>
<p>This is all the code for our layer! As you can see, this just creates a simple sprite (don&#8217;t forget <em>DragSprite</em> inherits <em>CCSprite</em>). It places it randomly on the X and Y axis of the device&#8217;s screen. Also, note that we don&#8217;t have any arrays keeping track of sprites. We don&#8217;t need one because the sprites will keep track of themselves, and handle their own touches. They implement the <em>CCTargetedTouchDelegate</em> protocol.</p>
<p>Now, let&#8217;s get to the meat of this article, the <em>DragSprite</em> class. I will again start with the init, which consists of a single line of code :</p>
<pre class="php">
-<span class="phpOperator">(</span>id<span class="phpOperator">)</span> init<span class="phpOperator">{</span>
<span class="phpKeyword">	if<span class="phpOperator">(</span></span><span class="phpOperator">(</span>self=<span class="phpOperator">[</span>super init<span class="phpOperator">]</span><span class="phpOperator">)</span><span class="phpOperator">)</span><span class="phpOperator">{</span>
		<span class="phpOperator">[</span><span class="phpOperator">[</span>CCTouchDispatcher sharedDispatcher<span class="phpOperator">]</span> addTargetedDelegate<span class="phpOperator">:</span>self priority<span class="phpOperator">:</span><span class="phpNumber">0</span> swallowsTouches<span class="phpOperator">:</span>YES<span class="phpOperator">]</span><span class="phpText">;</span>
	<span class="phpOperator">}</span>
<span class="phpKeyword">	return </span>self<span class="phpText">;</span>
<span class="phpOperator">}</span>
</pre>
<p><br/></p>
<p>Here, we tell the <em>CCTouchDispatcher</em> singleton that this sprite (<em>self</em>) wants to receive touch events. Note that it will receive ALL touch events, not just the ones touching the it. This is why we need to set priorities and check for a position match. In our case, all the sprites will have the same priority. <em>swallowsTouches</em> means that once we accept the touch (return YES on the touch began method), no other nodes will receive the touch. If we set this to NO, all touch delegates will be informed of the touch, wether others have accepted it or not. This can be useful for dragging multiple sprites at once for example.</p>
<p>Next, now that we added the node to the <em>CCTouchDispatcher</em>, we need to implement the touch methods. Here&#8217;s the first one, <em>ccTouchBegan</em>:</p>
<pre class="php">
- <span class="phpOperator">(</span>BOOL<span class="phpOperator">)</span>ccTouchBegan<span class="phpOperator">:</span><span class="phpOperator">(</span>UITouch *<span class="phpOperator">)</span>touch withEvent<span class="phpOperator">:</span><span class="phpOperator">(</span>UIEvent *<span class="phpOperator">)</span>event
<span class="phpOperator">{</span>
	CGPoint touchPoint <span class="phpOperator">=</span> <span class="phpOperator">[</span>touch locationInView<span class="phpOperator">:</span><span class="phpOperator">[</span>touch view<span class="phpOperator">]</span><span class="phpOperator">]</span><span class="phpText">;</span>
	touchPoint <span class="phpOperator">=</span> <span class="phpOperator">[</span><span class="phpOperator">[</span>CCDirector sharedDirector<span class="phpOperator">]</span> convertToGL<span class="phpOperator">:</span>touchPoint<span class="phpOperator">]</span><span class="phpText">;</span>
<span class="phpKeyword">	if<span class="phpOperator">(</span></span><span class="phpOperator">[</span>self isTouchOnSprite<span class="phpOperator">:</span>touchPoint<span class="phpOperator">]</span><span class="phpOperator">)</span><span class="phpOperator">{</span>
		isDrag<span class="phpOperator">=</span>YES<span class="phpText">;</span>
		whereTouch=ccpSub<span class="phpOperator">(</span>self<span class="phpOperator">.</span>position, touchPoint<span class="phpOperator">)</span><span class="phpText">;</span>
	<span class="phpKeyword">	return </span>YES<span class="phpText">;</span>
	<span class="phpOperator">}</span>
<span class="phpKeyword">	return </span>NO<span class="phpText">;</span>
<span class="phpOperator">}</span>
</pre>
<p><br/></p>
<p>The first 2 lines are just to get the touch in cocos2d coordinates. Then, we check if the touch is on the sprite. Note that the method <em>isTouchOnSprite</em> is not from cocos2d. It will be detailed next in this article. If the method returns YES, it means that the touch is on the sprite. Since it is, we return YES. if it is not, we return NO and the 2 other touch method, <em>touchMoved</em> and <em>touchEnded</em> will not be called. The touch will then be passed to all other nodes implementing the touch delegate. </p>
<p>You will also see that I set another variable, <em>whereTouch</em>. This is a simple <strong>CGPoint</strong> declared in the header file. Its goal is to know exactly where the touch is on the sprite, so when we move it, it won&#8217;t auto center to your finger. For example, if you grab it at it&#8217;s top left corner, it will be dragged from that point. You&#8217;ll understand when running the included project.</p>
<p>Now here&#8217;s the <em>isTouchOnSprite</em> method, which checks if the touch is on the sprite (remember that all nodes that receive touches receive ALL touches, wether it actually touches the concerned node or not).</p>
<pre class="php">
-<span class="phpOperator">(</span>BOOL<span class="phpOperator">)</span> isTouchOnSprite<span class="phpOperator">:</span><span class="phpOperator">(</span>CGPoint<span class="phpOperator">)</span>touch<span class="phpOperator">{</span>
<span class="phpKeyword">	if<span class="phpOperator">(</span></span>CGRectContainsPoint<span class="phpOperator">(</span>CGRectMake<span class="phpOperator">(</span>self<span class="phpOperator">.</span>position<span class="phpOperator">.</span>x - <span class="phpOperator">(</span><span class="phpOperator">(</span>self<span class="phpOperator">.</span>contentSize.width/<span class="phpNumber">2</span><span class="phpOperator">)</span>*self<span class="phpOperator">.</span>scale<span class="phpOperator">)</span>, self<span class="phpOperator">.</span>position<span class="phpOperator">.</span>y - <span class="phpOperator">(</span><span class="phpOperator">(</span>self<span class="phpOperator">.</span>contentSize.height/<span class="phpNumber">2</span><span class="phpOperator">)</span>*self<span class="phpOperator">.</span>scale<span class="phpOperator">)</span>, self<span class="phpOperator">.</span>contentSize.width*self<span class="phpOperator">.</span>scale, self<span class="phpOperator">.</span>contentSize.height*self<span class="phpOperator">.</span>scale<span class="phpOperator">)</span>, touch<span class="phpOperator">)</span><span class="phpOperator">)</span>
	<span class="phpKeyword">	return </span>YES<span class="phpText">;</span>
<span class="phpKeyword">	else </span>>return NO<span class="phpText">;</span>
<span class="phpOperator">}</span>
</pre>
<p><br/></p>
<p>The <em>CGRectContainsPoint</em> is a simple function that takes 2 parameters : a <strong>CGRect</strong> and a <strong>CGPoint</strong>. It returns YES if the point is in the rect and no if it is not. </p>
<p><em>CGRectMake</em> is a method that creates a <strong>CGRect</strong>, and takes 4 parameters : x, y, width and height. If CGRectContainsPoint returns YES, we return YES to our <em>ccTouchBegan</em> method to actually register the touch and receive the <em>touchMoved</em> and <em>touchEnded</em> calls, where we will move the sprite.</p>
<p>Now, here&#8217;s the <em>ccTouchMoved</em> selector. This will be called when we accepted the touch (returned YES on <em>ccTouchBegan</em>) and the finger moved. This is where we will move the sprite to the new touch position and add the <em>whereTouch</em> difference so the sprite doesn&#8217;t auto center to the finger&#8217;s position.</p>
<pre class="php">
- <span class="phpOperator">(</span>void<span class="phpOperator">)</span>ccTouchMoved<span class="phpOperator">:</span><span class="phpOperator">(</span>UITouch *<span class="phpOperator">)</span>touch withEvent<span class="phpOperator">:</span><span class="phpOperator">(</span>UIEvent *<span class="phpOperator">)</span>event
<span class="phpOperator">{</span>
	CGPoint touchPoint <span class="phpOperator">=</span> <span class="phpOperator">[</span>touch locationInView<span class="phpOperator">:</span><span class="phpOperator">[</span>touch view<span class="phpOperator">]</span><span class="phpOperator">]</span><span class="phpText">;</span>
	touchPoint <span class="phpOperator">=</span> <span class="phpOperator">[</span><span class="phpOperator">[</span>CCDirector sharedDirector<span class="phpOperator">]</span> convertToGL<span class="phpOperator">:</span>touchPoint<span class="phpOperator">]</span><span class="phpText">;</span>
	self<span class="phpOperator">.</span>position<span class="phpOperator">=</span>ccpAdd<span class="phpOperator">(</span>touchPoint,whereTouch<span class="phpOperator">)</span><span class="phpText">;</span>
<span class="phpOperator">}</span>
</pre>
<p><br/></p>
<p>As you can see, all we&#8217;re doing is changing the position of the sprite (<em>self</em>) to the addition of the new touch location + touch location on the sprite. This means that if we drag it from a corner, as soon as we move, the sprite wont center itself on the touch. If this is what you want, remove whereTouch from this project and it will work like that.</p>
<p>In <em>ccTouchEnded</em>, for the case of this project, we put nothing. We could have not even implemented it and it would work fine.</p>
<pre class="php">
- <span class="phpOperator">(</span>void<span class="phpOperator">)</span>ccTouchEnded<span class="phpOperator">:</span><span class="phpOperator">(</span>UITouch *<span class="phpOperator">)</span>touch withEvent<span class="phpOperator">:</span><span class="phpOperator">(</span>UIEvent *<span class="phpOperator">)</span>event
<span class="phpOperator">{</span>
<span class="phpOperator">}</span>
</pre>
<p><br/></p>
<p>I hope this article helped you better understand how touches work in cocos2d. Doing it this method, you could use multiple fingers and drag multiple sprites at the same time. This is the true way to do multitouch sprite dragging in Cocos2d.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.qcmat.com/dragging-multiple-sprites-in-cocos2d/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>SneakyInput &#8211; Joystick, dPad and buttons for cocos2d</title>
		<link>http://www.qcmat.com/sneakyinput-joystick-dpad-and-buttons-for-cocos2d/</link>
		<comments>http://www.qcmat.com/sneakyinput-joystick-dpad-and-buttons-for-cocos2d/#comments</comments>
		<pubDate>Wed, 28 Jul 2010 14:53:18 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[cocos2d]]></category>
		<category><![CDATA[Game development]]></category>
		<category><![CDATA[Interesting Stuff]]></category>

		<guid isPermaLink="false">http://www.qcmat.com/?p=79</guid>
		<description><![CDATA[When building games, one of the most visibly easy things to do, adding buttons and controls, is often a complicated task. Luckily, Nick Pannuto, a guy from the iPhone game developer community, released SneakyInput, a group of classes that include a joystick, a dPad and very good buttons. Created with simplicity and ease of use [...]]]></description>
			<content:encoded><![CDATA[<p>When building games, one of the most visibly easy things to do, adding buttons and controls, is often a complicated task. Luckily, Nick Pannuto, a guy from the iPhone game developer community, released SneakyInput, a group of classes that include a joystick, a dPad and very good buttons. Created with simplicity and ease of use in mind, they&#8217;re still some of the most complete and functional input classes.</p>
<p><img src="/qcblog/images/sneakyjoy.jpg"/></p>
<h3>Downlading SneakyInput</h3>
<p>Luckily for you, SneakyInput is an open source project, thus is free. Keep in mind giving donations to the creator if you make money using this kit. It surely will help getting updates and more content from him.</p>
<p>Start by downloading the package from this page : <a href="http://github.com/sneakyness/SneakyInput">Link</a>. To download, click on the big &#8220;Download Source&#8221; button at the top of the page.</p>
<p>When you unzip the package, you&#8217;ll see both the classes and a sample project. As you can see, there isn&#8217;t much there and all is pretty simple to understand as you&#8217;ll see.</p>
<h3>Usage</h3>
<p><br/></p>
<h4>Joystick / dPad</h4>
<p>In addition to the actual Joystick class, the developer included a class that creates colored circles dynamically. Instead of using sprites, for debugging reasons or else, you can use this class. The circles are just like sprites, but with programmed color instead of an actual texture. There are no resources needed to use it.</p>
<p>Start by including the necessary files : </p>
<pre class="php">
#import <span class="phpString">"SneakyJoystick<span class="phpOperator">.</span>h"</span>
#import <span class="phpString">"SneakyJoystickSkinnedBase.h"</span>
#import <span class="phpString">"ColoredCircleSprite.h"</span>
</pre>
<p><br/></p>
<p>Those are the needed files in order to use either the Joystick or the dPad. A dPad is basically a joystick, moving only on either X or Y axis, without 360 degrees rotation. It&#8217;s a &#8220;crippled&#8221; joystick, and is programmed almost in the same way.</p>
<p>In your target layer, the layer you want to add the Joystick to, start by creating the base of the joystick, which will hold the rest of the stuff.</p>
<pre class="php">
SneakyJoystickSkinnedBase *leftJoy <span class="phpOperator">=</span> <span class="phpOperator">[</span><span class="phpOperator">[</span><span class="phpOperator">[</span>SneakyJoystickSkinnedBase alloc<span class="phpOperator">]</span> init<span class="phpOperator">]</span> autorelease<span class="phpOperator">]</span><span class="phpText">;</span>
leftJoy<span class="phpOperator">.</span>position <span class="phpOperator">=</span> ccp<span class="phpOperator">(</span>64,64<span class="phpOperator">)</span><span class="phpText">;</span>
</pre>
<p><br/></p>
<p>The second line places the Joystick in the bottom left corner, because later in the tutorial, we&#8217;ll give it a 32 pixel radius.</p>
<p>After creating the Joystick, you have either of 2 options. You can:</p>
<p>-Use your own sprites<br />
-Use ColoredCircleSprite, a class that creates colored circles with <strong>RBGA</strong>.</p>
<p>In the first example, we will be using our own sprite, created from a simple PNG file.</p>
<pre class="php">
<span class="phpComment">// Sprite that will act<span class="phpKeyword"> as </span>the outter circle. Make this the same width<span class="phpKeyword"> as </span>joystick<span class="phpOperator">.</span>
</span>leftJoy<span class="phpOperator">.</span>backgroundSprite <span class="phpOperator">=</span> <span class="phpOperator">[</span>CCSprite spriteWithFile<span class="phpOperator">:</span>@<span class="phpString">"outerJoy<span class="phpOperator">.</span>png"</span><span class="phpOperator">]</span><span class="phpText">;</span>
<span class="phpComment">// Sprite that will act<span class="phpKeyword"> as </span>the actual Joystick<span class="phpOperator">.</span> Definitely make this smaller than outer circle.
</span>leftJoy<span class="phpOperator">.</span>thumbSprite <span class="phpOperator">=</span> <span class="phpOperator">[</span>CCSprite spriteWithFile<span class="phpOperator">:</span>@<span class="phpString">"innerJoy<span class="phpOperator">.</span>png"</span><span class="phpOperator">]</span><span class="phpText">;</span>
</pre>
<p><br/></p>
<p>On the other hand, for debugging purposes or if a simple RBGA Joystick is enough for your project, SneakyInput includes a class to create circles out of a radius and a RBGA code.</p>
<pre class="php">
leftJoy<span class="phpOperator">.</span>backgroundSprite <span class="phpOperator">=</span> <span class="phpOperator">[</span>ColoredCircleSprite circleWithColor<span class="phpOperator">:</span>ccc4<span class="phpOperator">(</span>255, <span class="phpNumber">0</span>, <span class="phpNumber">0</span>, 128<span class="phpOperator">)</span> radius<span class="phpOperator">:</span>32<span class="phpOperator">]</span><span class="phpText">;</span>
leftJoy<span class="phpOperator">.</span>thumbSprite <span class="phpOperator">=</span> <span class="phpOperator">[</span>ColoredCircleSprite circleWithColor<span class="phpOperator">:</span>ccc4<span class="phpOperator">(</span><span class="phpNumber">0</span>, <span class="phpNumber">0</span>, 255, 200<span class="phpOperator">)</span> radius<span class="phpOperator">:</span>16<span class="phpOperator">]</span><span class="phpText">;</span>
</pre>
<p><br/></p>
<p>In the previous code, instead of creating CCSprites to see the Joystick, we&#8217;re using a special class called <strong>ColoredCircleSprite</strong>. As simple as can be, this class takes 2 parameters: a <em>ccc4 </em>color (R,B,G,A -> meaning Red, Blue, Green, Alpha) and a <em>radius</em>, which is half the width of the wanted Joystick.</p>
<p>Then, whether you used <strong>ColorCircleSprite </strong>or a regular cocos2d <strong>CCSprite</strong>, the next part is the same. We&#8217;re gonna be adding the actual Joystick do our Joystick base.</p>
<pre class="php">
leftJoy<span class="phpOperator">.</span>joystick <span class="phpOperator">=</span> <span class="phpOperator">[</span><span class="phpOperator">[</span>SneakyJoystick alloc<span class="phpOperator">]</span> initWithRect<span class="phpOperator">:</span>CGRectMake<span class="phpOperator">(</span><span class="phpNumber">0</span>,0,128,128<span class="phpOperator">)</span><span class="phpOperator">]</span><span class="phpText">;</span>
leftJoystick <span class="phpOperator">=</span> <span class="phpOperator">[</span>leftJoy<span class="phpOperator">.</span>joystick retain<span class="phpOperator">]</span><span class="phpText">;</span>
<span class="phpOperator">[</span>self addChild<span class="phpOperator">:</span>leftJoy<span class="phpOperator">]</span><span class="phpText">;</span>
</pre>
<p><br/></p>
<p>In the first line, we assign a Joystick to the <strong>joystick</strong> property of the Joystick Base. The <strong>SneakyJoystick </strong>class actually only takes 1 parameter, it&#8217;s <em>size </em>and <em>position</em>. This parameter is in the form of a <strong>CGRect </strong>: <em>CGRectMake(int x, int y, int width, int height)</em>. The width and height are full widths and heights, not to mix with the radius of the previous code snippets.</p>
<p>The second line assigns a value to our <strong>SneakyJoystick </strong>property that we need to keep in order to query the Joystick at a specified interval.</p>
<p>In the last line, just like any other cocos2d example, we add the Joystick to the <strong>layer</strong>. Without this, the Joystick will not be visible.</p>
<h4>Usage</h4>
<p>The <strong>SneakyJoystick </strong>is not delegated. This means that in order to use it and apply its movement to <strong>CCSprites </strong>or any other object, you need to query it. This can be done anywhere, anytime. The suggested usage is to create a selector that will be called every frame and do all your stuff there.</p>
<pre class="php">
<span class="phpOperator">[</span>self schedule<span class="phpOperator">:</span>@selector<span class="phpOperator">(</span>tick<span class="phpOperator">:</span><span class="phpOperator">)</span><span class="phpOperator">]</span><span class="phpText">;</span>
</pre>
<p> <br/></p>
<p>Now the selector would look something like this:</p>
<pre class="php">
-<span class="phpOperator">(</span>void<span class="phpOperator">)</span>tick<span class="phpOperator">:</span><span class="phpVarType"><span class="phpOperator">(</span>float<span class="phpOperator">)</span></span>delta <span class="phpOperator">{</span>
<span class="phpComment">/* This will take the joystick and tell a special method <span class="phpOperator">(</span>not listed here, outside the scope of this guide<span class="phpOperator">)</span> to take the joystick, apply movement to hero <span class="phpOperator">(</span>CCSprite or<span class="phpKeyword"> else<span class="phpOperator">)</span></span> and apply the real delta <span class="phpOperator">(</span>to avoid uneven or choppy movement, delta is the time since the last time the method was called, in milliseconds<span class="phpOperator">)</span>. */</span>
<span class="phpOperator">[</span>self applyJoystick<span class="phpOperator">:</span>leftJoystick toNode<span class="phpOperator">:</span>hero forTimeDelta<span class="phpOperator">:</span>delta<span class="phpOperator">]</span><span class="phpText">;</span>
<span class="phpOperator">}</span>
</pre>
<p><br/></p>
<p>This concludes how to use <strong>SneakyJoystick</strong>! Now you&#8217;re probably wondering how do turn this into an awesome dPad for your fighting game or whatever creative game you&#8217;re tinkering up. If you&#8217;re hoping for a quick one line solution, you&#8217;re in luck! Here&#8217;s what to do to turn the Joystick into a dPad :</p>
<pre class="php">
joystick<span class="phpOperator">.</span>isDPad <span class="phpOperator">=</span> YES<span class="phpText">;</span>
</pre>
<p><br/></p>
<p>That&#8217;s all there is to it! Other than that, using sprites that looks like an actual dPad will surely give it a more oldschool and realistic look and feel. </p>
<h4>Buttons</h4>
<p>Now that you can handle the joystick, which is the most complicated of the duo, it&#8217;s time to add some buttons. Buttons from <strong>SneakyInput </strong>are much more complete and usable in a game as buttons from cocos2d (<strong>CCMenuItem</strong>).</p>
<p>First, we need to include the necessary files in order to use the buttons.</p>
<pre class="php">
#import <span class="phpString">"SneakyButton<span class="phpOperator">.</span>h"</span>
#import <span class="phpString">"SneakyButtonSkinnedBase.h"</span>
</pre>
<p><br/></p>
<p>The buttons are created and added to the layer in the same way as we did with the joystick. In the following example, we&#8217;ll be using the <strong>ColoredCircleSprite </strong>class. Don&#8217;t forget that you can easily switch those for regular <strong>CCSprites</strong></p>
<pre class="php">
SneakyButtonSkinnedBase *rightBut <span class="phpOperator">=</span> <span class="phpOperator">[</span><span class="phpOperator">[</span><span class="phpOperator">[</span>SneakyButtonSkinnedBase alloc<span class="phpOperator">]</span> init<span class="phpOperator">]</span> autorelease<span class="phpOperator">]</span><span class="phpText">;</span>
rightBut.position <span class="phpOperator">=</span> ccp<span class="phpOperator">(</span>448,32<span class="phpOperator">)</span><span class="phpText">;</span>
rightBut.defaultSprite <span class="phpOperator">=</span> <span class="phpOperator">[</span>ColoredCircleSprite circleWithColor<span class="phpOperator">:</span>ccc4<span class="phpOperator">(</span>255, 255, 255, 128<span class="phpOperator">)</span> radius<span class="phpOperator">:</span>32<span class="phpOperator">]</span><span class="phpText">;</span>
rightBut.activatedSprite <span class="phpOperator">=</span> <span class="phpOperator">[</span>ColoredCircleSprite circleWithColor<span class="phpOperator">:</span>ccc4<span class="phpOperator">(</span>255, 255, 255, 255<span class="phpOperator">)</span> radius<span class="phpOperator">:</span>32<span class="phpOperator">]</span><span class="phpText">;</span>
rightBut.pressSprite <span class="phpOperator">=</span> <span class="phpOperator">[</span>ColoredCircleSprite circleWithColor<span class="phpOperator">:</span>ccc4<span class="phpOperator">(</span>255, <span class="phpNumber">0</span>, <span class="phpNumber">0</span>, 255<span class="phpOperator">)</span> radius<span class="phpOperator">:</span>32<span class="phpOperator">]</span><span class="phpText">;</span>
rightBut.button <span class="phpOperator">=</span> <span class="phpOperator">[</span><span class="phpOperator">[</span>SneakyButton alloc<span class="phpOperator">]</span> initWithRect<span class="phpOperator">:</span>CGRectMake<span class="phpOperator">(</span><span class="phpNumber">0</span>, <span class="phpNumber">0</span>, 64, 64<span class="phpOperator">)</span><span class="phpOperator">]</span><span class="phpText">;</span>
rightButton <span class="phpOperator">=</span> <span class="phpOperator">[</span>rightBut.button retain<span class="phpOperator">]</span><span class="phpText">;</span>
rightButton<span class="phpOperator">.</span>isToggleable <span class="phpOperator">=</span> YES<span class="phpText">;</span>
<span class="phpOperator">[</span>self addChild<span class="phpOperator">:</span>rightBut<span class="phpOperator">]</span><span class="phpText">;</span>
</pre>
<p><br/></p>
<p>As you can see, instead of only using 2 sprites, the button uses 3. The 3rd sprite being used is only displayed when the button is set to <em>isToggleable </em>(will explain later). This means that it&#8217;ll only show when the button is selected.</p>
<p>At the end of the code, we set <em>isToggleable </em>to YES. This means the button will act as a switch. It&#8217;ll activate or deactivate on touch, just like a light switch.</p>
<p>To use the button, you will need to use the same technique as the joystick, meaning querying the buttons, whenever you want, to get their values. Here are the different possibilities:</p>
<pre class="php">
<span class="phpComment">// pressed
</span>>if <span class="phpOperator">(</span>rightButton<span class="phpOperator">.</span>active <span class="phpOperator"><span class="phpOperator">=</span>=</span> YES<span class="phpOperator">)</span><span class="phpOperator">{</span>
    <span class="phpOperator">[</span>this doThat<span class="phpOperator">]</span><span class="phpText">;</span> <span class="phpComment">// Button is pressed at the moment
</span><span class="phpOperator">}</span>
<span class="phpKeyword">
else<span class="phpOperator">{</span></span>
    <span class="phpOperator">[</span>this doElse<span class="phpOperator">]</span><span class="phpText">;</span> <span class="phpComment">// Button is NOT pressed at the moment
</span><span class="phpOperator">}</span>
<span class="phpComment">// switched <span class="phpOperator">(</span>only<span class="phpKeyword"> if </span>isToggleable is set to YES<span class="phpOperator">)</span>
</span>>if <span class="phpOperator">(</span>rightButton<span class="phpOperator">.</span>value <span class="phpOperator"><span class="phpOperator">=</span>=</span> <span class="phpNumber">1</span><span class="phpOperator">)</span><span class="phpOperator">{</span>
    <span class="phpOperator">[</span>this doThat<span class="phpOperator">]</span><span class="phpText">;</span> <span class="phpComment">// Button is activated
</span><span class="phpOperator">}</span>
<span class="phpKeyword">
else<span class="phpOperator">{</span></span>
    <span class="phpOperator">[</span>this doElse<span class="phpOperator">]</span><span class="phpText">;</span> <span class="phpComment">// Button is NOT activated
</span><span class="phpOperator">}</span>
</pre>
<p><br/></p>
]]></content:encoded>
			<wfw:commentRss>http://www.qcmat.com/sneakyinput-joystick-dpad-and-buttons-for-cocos2d/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>Understanding anchorPoint in cocos2d</title>
		<link>http://www.qcmat.com/understanding-anchorpoint-in-cocos2d/</link>
		<comments>http://www.qcmat.com/understanding-anchorpoint-in-cocos2d/#comments</comments>
		<pubDate>Mon, 26 Jul 2010 14:39:19 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[cocos2d]]></category>
		<category><![CDATA[anchor]]></category>
		<category><![CDATA[anchorPoint]]></category>
		<category><![CDATA[position]]></category>
		<category><![CDATA[rotation]]></category>

		<guid isPermaLink="false">http://www.qcmat.com/?p=53</guid>
		<description><![CDATA[There&#8217;s something in cocos2d which is super easy to understand, but that most people seem to have problems with it at first : Anchor Points. Every class derived from CCNode (Ultimate cocos2d class) will have a anchorPoint property. When determining where to draw the object (sprite, layer, whatever), cocos2d will combine both properties position and [...]]]></description>
			<content:encoded><![CDATA[<p>There&#8217;s something in cocos2d which is super easy to understand, but that most people seem to have problems with it at first : Anchor Points. Every class derived from <strong>CCNode </strong>(Ultimate cocos2d class) will have a <strong>anchorPoint</strong> property.</p>
<p>When determining where to draw the object (sprite, layer, whatever), cocos2d will combine both properties <strong>position </strong>and <strong>anchorPoint</strong>. Also, when rotating an object, cocos2d will rotate it around its <strong>anchorPoint</strong>, as if a pin were placed on that very spot.</p>
<h2>Positioning</h2>
<p>As a first example, this sprite has an <strong>anchorPoint </strong>of <em>ccp(0,0)</em> and a position of <em>ccp(0,0)</em>. This rectangle sprite will be placed at the bottom left corner of its parent, the layer.</p>
<pre class="php">
CCSprite *sprite <span class="phpOperator">=</span> <span class="phpOperator">[</span>CCSprite spritewithFile<span class="phpOperator">:</span>@<span class="phpString">"blackSquare.png"</span><span class="phpOperator">]</span><span class="phpText">;</span>
sprite.position<span class="phpOperator">=</span>ccp<span class="phpOperator">(</span><span class="phpNumber">0</span>,0<span class="phpOperator">)</span><span class="phpText">;</span>
sprite.anchorPoint<span class="phpOperator">=</span>ccp<span class="phpOperator">(</span><span class="phpNumber">0</span>,0<span class="phpOperator">)</span><span class="phpText">;</span>
<span class="phpOperator">[</span>self addChild<span class="phpOperator">:</span>sprite<span class="phpOperator">]</span><span class="phpText">;</span>
</pre>
<p><br/><br />
<img src="/qcblog/images/anchor1.JPG"/><br />
<br/><br />
In this second example, we will assign a <strong>anchorPoint </strong>of <em>ccp(-1,-1)</em> to better understand the relative value of the anchor point.</p>
<pre class="php">
CCSprite *sprite <span class="phpOperator">=</span> <span class="phpOperator">[</span>CCSprite spritewithFile<span class="phpOperator">:</span>@<span class="phpString">"blackSquare.png"</span><span class="phpOperator">]</span><span class="phpText">;</span>
sprite.position<span class="phpOperator">=</span>ccp<span class="phpOperator">(</span><span class="phpNumber">0</span>,0<span class="phpOperator">)</span><span class="phpText">;</span>
sprite.anchorPoint<span class="phpOperator">=</span>ccp<span class="phpOperator">(</span>-<span class="phpNumber">1</span>,-<span class="phpNumber">1</span><span class="phpOperator">)</span><span class="phpText">;</span>
<span class="phpOperator">[</span>self addChild<span class="phpOperator">:</span>sprite<span class="phpOperator">]</span><span class="phpText">;</span>
</pre>
<p><br/><br />
<img src="/qcblog/images/anchor2.JPG"/><br />
<br/><br />
As you can see in the image, the anchor point is <strong>not a pixel value</strong>. The value of <strong>X</strong> and <strong>Y</strong> are relative to the size of the node. For example, in a 30&#215;30 pixel square, something like the image above, a <strong>anchorPoint </strong>of <em>ccp(-1,-1)</em> will mean &#8220;<i>Place the anchor 1 * myWidth to the left and 1 * myHeight under the sprite</i>&#8221;</p>
<p>As a final example, to better understand the combination of <strong>position </strong>and <strong>anchorPoint</strong>, I&#8217;ll show you different lines of code that give exactly the same result as the first image. That piece of code will combine both <strong>position </strong>and <strong>anchorPoint</strong>, and show you how to use the <strong>contentSize </strong>property.</p>
<pre class="php">
CCSprite *sprite <span class="phpOperator">=</span> <span class="phpOperator">[</span>CCSprite spritewithFile<span class="phpOperator">:</span>@<span class="phpString">"blackSquare.png"</span><span class="phpOperator">]</span><span class="phpText">;</span>
sprite.anchorPoint<span class="phpOperator">=</span>ccp<span class="phpOperator">(</span><span class="phpNumber">1</span>,1<span class="phpOperator">)</span><span class="phpText">;</span>
sprite.position<span class="phpOperator">=</span>ccp<span class="phpOperator">(</span>sprite.contentSize.width , sprite.contentSize.height<span class="phpOperator">)</span><span class="phpText">;</span>
<span class="phpOperator">[</span>self addChild<span class="phpOperator">:</span>sprite<span class="phpOperator">]</span><span class="phpText">;</span>
</pre>
<p>Using this snippet, we place the <strong>anchorPoint </strong>at <em>ccp(1,1)</em>, which places it 1 * width to the right and 1 * height over. If we kept <strong>position </strong>at <em>ccp(0,0)</em>, the sprite would be outside the screen, to the bottom left of it. </p>
<p>By placing the sprite at a <strong>position </strong>equal to ccp(1 * width, 1* height), it returns to the origin, at the bottom left corner.<br />
<br/><br />
<img src="/qcblog/images/anchor1.JPG"/><br />
<br/></p>
<h2>Rotation</h2>
<p>When rotating, we need to be careful with <strong>anchorPoint</strong>, as it can give us some very weird results, more so when using irregular anchor points such as <em>ccp(0,0)</em>. By default, the <strong>anchorPoint </strong>is set to <em>ccp(0.5f,0.5f)</em>, which is the middle of the sprite on both <strong>X </strong>and <strong>Y </strong>axes (remember that <strong>anchorPoint </strong>is a relative value to the size of the sprite).</p>
<p>Using <em>ccp(0.5f,0.5f)</em> as an <strong>anchorPoint </strong>will give the simplest rotation possible, which doesn&#8217;t change the actual position of the sprite. It&#8217;ll rotate just as you would normally think.</p>
<p>In the following example, the <strong>anchorPoint </strong>is set to <em>ccp(1,1)</em>, meaning the sprite will rotate around its top right corner.<br />
<br/><br />
<img src="/qcblog/images/rotate1.JPG"/><br />
<br/><br />
Remember that since <strong>anchorPoint </strong>is relative, you can assign it to <em>ccp(2,2)</em> and the sprite will rotate around that point. This is the case in the following example.<br />
<br/><br />
<img src="/qcblog/images/rotate2.JPG"/><br />
<br/></p>
<h2>Simpler than it looks</h2>
<p>I hope this tutorial was clear and helped you better understand how to use anchor points in cocos2d, and combine them with the position to place the sprites where you like, how you like. Also, by using different anchor points, you learned that it is possible to create different rotating patterns, such as rotating around external points.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.qcmat.com/understanding-anchorpoint-in-cocos2d/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Welcome to QcMat.com</title>
		<link>http://www.qcmat.com/welcome-to-qcmat-com/</link>
		<comments>http://www.qcmat.com/welcome-to-qcmat-com/#comments</comments>
		<pubDate>Wed, 21 Jul 2010 00:32:29 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.qcmat.com/?p=5</guid>
		<description><![CDATA[Hello, Welcome to QcMat.com, where I will blog about my game development and add tutorials when the need comes. I love helping people and regularly am in a chat room helping people. I think that by putting this information in a blog or website, it can help loads of other people with the same problems [...]]]></description>
			<content:encoded><![CDATA[<p>Hello,</p>
<p>Welcome to QcMat.com, where I will blog about my game development and add tutorials when the need comes. I love helping people and regularly am in a chat room helping people. I think that by putting this information in a blog or website, it can help loads of other people with the same problems or questions.</p>
<p>I hope you enjoy my blog, and feel free to follow me on twitter @QcMatDev.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.qcmat.com/welcome-to-qcmat-com/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

