Collision Detection In Box2D – Using With Cocos2D

For an interactive game building using Box2D, collision detection of Box2D bodies is necessary. There is an easy way to implement collision detection in Box2D.

For checking whether two bodies have collided with each other in Box2D while using it with cocos2D, then use the following lines of code where ever you want to check collision.

if((contact.fixtureA == sourceFixture && contact.fixtureB == destinationFixture) ||
(contact.fixtureA == destinationFixture && contact.fixtureB == sourceFixture ))
{
//What you want to do after checking the collision?!!!
//That lines of code goes here!
//Simple and Pretty , isn't it?
}

Here you are cheking whether two box2D fixtures are in contact or not! And you can decide which fixtures you need to check for collision.
:)

Removing Forces From Your World in Box2D Cocos2D

Hi,

In certain situations you may need to remove all the forces you have applied on different bodies in your box2D world.
Then what to do?
Removing all the forces from the bodies in the box2D world is simple. Just use the single line of code.

yourWorld->ClearForces();

Here yourWorld is your Box2D world. Simple, isn’t it?
:)

File Operations in Adobe AIR/FLEX

The following example shows how to deal with a simple text file in Adobe AIR/FLEX.
This example illustratesd how to write a string to a text file and save it in the desktop.
However you can use the “application directory” but make sure that you have necessary write permission to access the directory.




	

Please leave your valuable comments…..

How to turnOff suggestions in editText in ANDROID?

Sometimes we may not need suggestions to appear in the textbox in our application in ANDROID.
This simple codes turns off this suggestion ….
Put this code inside your editText tag in the layout xml file.

android:inputType="textNoSuggestions"

if the above code is not working try this…

android:inputType="textNoSuggestions|textVisiblePassword"

Please leave your valuable comments………..

Creating a JMenubar in java swing

Creating a JMenubar is simple.

        JMenuBar  menubar = new JMenuBar();
	JMenu menu1 = new JMenu("File");
	JMenu menu2= new JMenu("Help");
	file = new JMenuItem("Exit to menu");
	edit = new JMenuItem("Exit");

	JMenuItem about = new JMenuItem("About us");
	f.setJMenuBar(menubar);
	menu1.add(file);
	menu1.add(edit);
	menu2.add(about);
	menubar.add(menu1);
	menubar.add(menu2);

	file.addActionListener(new ActionListener()
	{
		public void actionPerformed(ActionEvent arg0)
		{
			//Action......
		}
	});
	edit.addActionListener(new ActionListener()
	{
		public void actionPerformed(ActionEvent arg0)
		{
			//Action.....
		}
	});
	about.addActionListener(new ActionListener()
	{
		public void actionPerformed(ActionEvent arg0)
		{
			//Action....
		}
	});

Unscheduling All Selector Functions in Cocos2D iPhone

There may arise certain situations there you are using a number of @selector functions and want to stop all those selectors. In cocos2D there is an easy way to stop/unschedule all the selectors with a single line of code!

Use this line of code where ever you want to unshedule all the selectors being activated.

[self unscheduleAllSelectors];

:)

Play an MP3 in Adobe AIR/FLEX

Often in out applications we need to play sounds , so this code explains how to play, stop and resume an mp3 sound in Adobe AIR/FLEX.
What you have to do is to copy the following code and place it inside your “mx:Script” tag.
After that place three buttons for play, pause and stop and connect it to the three functions coded below.
Make sure that you have the sound file in your applications directory.


import adobe.utils.CustomActions;
import air.net.URLMonitor;
import flash.media.Sound;
import flash.media.ID3Info;
import flash.net.URLRequest;

var sound:Sound = new Sound(new URLRequest("app:/my_sound.mp3"));
var my_channel:SoundChannel;
var pausePos:int = 0;
[Bindable]
private function play():void
{
       my_channel= sound.play();
}
private function stop():void
{
      pausePosition = my_channel.position;
      my_channel.stop();
}
private function resume():void
{
       my_channel = sound.play(pausePos);
}

Please leave your valuable comments.

How to load a PDF in Titanium?

Loading a PDF file in Titanium may be sometimes our requirement for an application. For loading a PDF we need to have a webview inside a scrollview so that it is scrollable also. Take a look at this simple example.

/** creating a window **/
var window = Titanium.UI.createWindow{};
/** creating a webview **/
var my_webview = Titanium.UI.createWebView({     url :'my_pdf.pdf'      //specify the PDF path.....});
/** creating a scrollview **/var my_scrollView = Titanium.UI.createScrollableView({     views:[my_webview]   // placing the webview inside the scrollview...});
window.add(scrollView);

Please leave your valuable comments if this post was useful…..

Load a webpage in Adobe AIR

This example shows how to load a webpage inside Adobe AIR,
For this first you have to place an HTML control in your design for loading the html content.
Create an page “index.html” inside the application directory of your current project and add some Html content to it.
After that copy the following code to your source file.

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" applicationComplete="init()">
<mx:Script>
<![CDATA[
      private function init():void
      {
         this.stage.nativeWindow.maximize();    // maximizes your window.
         html.location="index.html";            // loads the html content in the html text area.
      }
]]>
</mx:Script>
<mx:HTML id="html"     width="100%" height="100%" />
</mx:WindowedApplication>

Please leave your comments on this post…….

Dynamically Load CSS in Adobe AIR/FLEX / Load css in Adobe AIR/FLEX using Class.

This example shows how to dynamically load css in Adobe AIR / FLEX.
Here in this example Panel id the tag name for the panel control. Drag a panel control to your design then copy the code to your source file. “myTabs” is the class for the CSS.




	Panel {
   borderColor:  #CBE0FF;
   border-style:solid;
   borderThickness: 1;
   borderThicknessLeft: 0;
   borderThicknessTop: 0;
   borderThicknessBottom: 0;
   borderThicknessRight: 0;
   cornerRadius: 4;
   highlightAlphas: 1, 0.49;
   headerColors: #0066ff, #ffffff;
   backgroundColor: #F5FAFF;
}
TitleWindow{
	borderColor:#CBE0FF;
   border-style:solid;
   borderThickness: 1;
   borderThicknessLeft: 0;
   borderThicknessTop: 0;
   borderThicknessBottom: 0;
   borderThicknessRight: 0;
   cornerRadius: 4;
   backgroundAlpha: 1;
   highlightAlphas: 1, 0.49;
   headerColors: #0066ff, #ffffff;
   backgroundColor: #F5FAFF;
   footerColors: #0099ff, #ffffff;
}

ComboBox
{
	cornerRadius: 4;
  	highlightAlphas: 0.52, 0.52;
	fillAlphas: 0, 1, 1, 1;
	fillColors: #0066ff, #ffffff, #ff9900, #eeeeee;
	borderColor: #cccccc;
	font-family:arial;
	font-size:11px;
	font-weight:normal;
	color:#000000;
}

ApplicationControlBar {
   highlightAlphas: 1, 0.42;
   fillAlphas: 0.78, 0.8;
   fillColors: #619ffc, #ffffff;
   backgroundAlpha: 1;
   cornerRadius: 1;
   shadowDistance: 1;
}
TabBar {
	tab-style-name: "myTabs";
	selected-tab-text-style-name: "mySelectedTabs";
    padding-left:0px;
	padding-right:0px;
	padding-top:0px;
	padding-bottom:0px;
	vertical-gap:0px;
	horizontal-gap:0px;
}
.myTabs {
   cornerRadius: 4;
   highlightAlphas: 0.30, 0;
   fillAlphas: 0.60, 0.40;
   /* fillColors: #c0dcf9, #e8f0f8; */
   fillColors: #95d0fe, #95d0fe;
   borderColor: #ffffff;
   border-thickness:4px;
   color: #0b333c;
   fontFamily: Arial;
   font-weight:bold;
   font-size:11px;
   backgroundColor: #f8d49d;
   padding-left:1;
   text-align:left;
   horizontal-gap:4px;
   vertical-align:middle;

}

.mySelectedTabs {
   textAlign: left;
   fontFamily: Arial;
   fontWeight: normal;
}
HBox{
 backgroundColor: #ffffff;
}


	
	
		
	
	
	
	
	
		
			
			
			
		
	
	
		
	
	
	
	
		
		
		
		
	
	
	
	
	
		
	
	
	
	


Client Side Form Validation – using JavaScript

Hi, JavaScript is mainly used for client side scripting. In client side scripting one of the thing which we are usually in need is Validation of Forms. We want to know the user , who is interacting with the form have already given the necessary details before submitting or saving etc. Here is a simple form validation example using JavaScript where the form is checked for a particular text field have entered or not. Here is the form code

First name:

It contains a Text field to enter first name and a button to save.
On clicking the Save button it will go for ‘validateForm()’ function and checks for user entry.

validateForm() code

[/css]
function validateForm()
{
var x=document.forms["trialForm"]["firstName"].value
if (x==null || x=="")
  {
  alert("Enter your first name before saving!");
  return false;
  }
}

This function checks for it & returns alert if it is not filled properly! :)

Using ButtonBar in Adobe AIR/FLEX, A simple Example

Hello……….

A button Bar is a convenient way to place your buttons in an application. It saves a lots of space in your application interface.
Take a look at the following example which shows how to use the ButtonBar. An itemClickEvent is attached to each button in the ButtonBar.
You can place as many buttons inside a button Bar.
Copy the following code to your MXML file and you are done…….
Happy Coding………..

<?xml version="1.0"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" >
    <mx:Script>
        <![CDATA[
            import mx.events.ItemClickEvent;

            private function clickHandler(event:ItemClickEvent):void {
                    myTA.text="Selected button index: " +
                    String(event.index) + "n" +
                    "Selected button label: " +
                    event.label;
            }
        ]]>
    </mx:Script>

    <mx:ToggleButtonBar  borderStyle="solid"  horizontalGap="5"  itemClick="clickHandler(event);"
    	    toggleOnClick="true"  selectedIndex="-1">
        <mx:dataProvider>
            <mx:String>Button1</mx:String>
            <mx:String>Button2</mx:String>
            <mx:String>Button3</mx:String>
            <mx:String>Button4</mx:String>
        </mx:dataProvider>
    </mx:ToggleButtonBar>
    <mx:TextArea id="myTA" width="250" height="100"/>
</mx:WindowedApplication>

Please leave your comments on this post.

How to load a URL in Objective C or Cocoa in iPhone?/ Open Safari in Objective C

This code snippet helps you to open a webpage in Safari.
Code uses openURL to load the webpage in the web browser.
Write this function as an action to a button………

- ( IBAction ) loadWebPageInSafari : ( id ) sender
{
          NSURL *my_URL = [NSURL URLWithString:[Your_URL stringValue]];
          if ( [ [NSWorkspace sharedWorkspace] openURL : my_URL ] ){
                   NSLog(@” URL Loaded................”);
         }else{
                   NSLog(@" Failed to Load URL.....");
         }
}

Using Arrays in JavaScript – Basics

Hi…

We all know, Arrays are nothing but variables which can hold all your variable values with a single name.

Let’s see some basic operations of Arrays in JavaScript.

To Create a New Array. Use the following code.

 var myArray = new Array();

This code of line created a new Array object called myArray.

Adding Elements to myArray

myArray[0]= "you";
myArray[1] = "me";
myArray[2] = "we";

or

var myArray=new Array("you","me","we"); 

or

var myArray=["you","me","we"];

Accessing Array Elements

document.write(myArray[0]);

It will print ‘you’!
:)

How to get a selected Item from a spinner in ANDROID?

We have come across this issue many times during programming to get a selected item in your combobox.
ANDROID has built in functions to get the selected item from a spinner.
Take a look at the snippet.
Here my_spinner id the spinner variable and using getSelectedItem() which will return an object and by using toString() I am converting it to human readable string…

my_spinner.getSelectedItem().toString();

Please leave your valuable comments…..

How to add more than One HTML Document in a single Browser ?

Hi…

There may arise certain situations where you need to include more than one HTML document in the same browser.

But how?

It’s simple. It’s by using FRAMES of HTML.
FRAMES can be included inside FRAMESET.

Let’s see an example.

<frameset cols="200,400">
   <frame src="http://www.coderzheaven.com" />
   <frame src="http://www.google.com" />
</frameset>

This will create two column frames in the browser with www.coderzheaven in one frame with 200 pixels, and www.google.com in another frame with 400 pixel.

You can also give cols percentage as well. Like cols=”25%,75%”.
:)

ProgressBar in ANDROID…..

Progress bars are our basic needs when we want to show something for a long time or some progress takes a long time and we want to show it’s progress.
Keeping that in mind ANDROID also has built in progress views.
There are basically two types of progress views in ANDROID.

ProgressDialog.STYLE_SPINNER and ProgressDialog.STYLE_HORIZONTAL

Take a look at this simple example that loads a progressBar on “onCreate”….

public class Example extends Activity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
    	super.onCreate(savedInstanceState);
    	setContentView(R.layout.main);
    	ProgressDialog dialog = ProgressDialog.show(Example.this, "", "Loading...", true);
    }
}

Toggle between Full Screen and normal Screen in Adobe AIR or FLEX.

Hi all………

We often need to toggle between fullscreen and normal screen in our application.
The following code snippet helps you to toggle between these two screens in Adobe AIR or Flex.
Here I am using StageDisplayState class to do both which has “stage.displayState = StageDisplayState.FULL_SCREEN” and ” stage.displayState = StageDisplayState.NORMAL” constants to do this.

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"  applicationComplete="toggleScreen()">
       <mx:Script>
               <![CDATA[
                       import flash.display.StageDisplayState;
                       private function toggleScreen():void{
                               if(fullScreen.selected == true){
                                       this.goFullScreen();
                               } else {
                                       this.exitFullScreen();
                               }
                       }
                       private function goFullScreen():void {
                               stage.displayState = StageDisplayState.FULL_SCREEN;
                       }
                       private function exitFullScreen():void {
                               stage.displayState = StageDisplayState.NORMAL;
                       }
               ]]>
       </mx:Script>
       <mx:CheckBox label="Full Screen" id="fullScreen" click="this.toggleScreen()"
               	               selected="true"  horizontalCenter="0" verticalCenter="0"/>
</mx:WindowedApplication>
 

How to detect shake Gesture in your iPhone Cocos2D?

For detecting shake in a cocos2D program copy these lines to your layer class

bool shaked_once; //default false

self.isAccelerometerEnabled = YES;
[[UIAccelerometer sharedAccelerometer] setUpdateInterval:1/60];
shaked_once = false;

Then copy this function to the same file…. and you are done……..

-(void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {

            float THRESHOLD = 2;

        if (acceleration.x > THRESHOLD || acceleration.x < -THRESHOLD ||
                          acceleration.y > THRESHOLD || acceleration.y < -THRESHOLD ||
                                      acceleration.z > THRESHOLD || acceleration.z < -THRESHOLD) {
                    if (!shaked_once) {
                           shaked_once = true;
                   }
         }
         else {
                          shaked_once = false;
         }
}

Hide and Show a Text Using jQuery – Example

With jQuery manipulation of HTML & CSS can be done very easily.
Here we are going to show how to Hide a line of text using jQuery & Show it back also.
Let’s code speak first….


<html>
<head>

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">

$(document).ready(function(){
$("#hideButton").click(function(){
$("p").hide();
});

$("#showButton").click(function(){
$("p").show();
});
});
</script>

</head>
<body>

<p>Hey! Click 'Left', I will vanish! Click 'Right', I will be back!</p>

<button id="hideButton">Left</button>
<button id="showButton">Right</button>
</body>
</html>

Note : Download and put ‘jquery.js’ on your working folder before trying out!

What these code did?

Here we have one line text, html paragraph. Two buttons ‘Left’ & ‘RIght’ and whose id’s

are ‘hideButton’ & ‘showButton’ respectively.

jQuery code identifies which button we clicked using the button id’s mentioned above and

apply the hide() or show() function to the html paragraph p.
:) 

Preventing Overriding in Java

Methods and variables can be ‘override’ in subclasses. (Yes, it’s a good

feature too!). But what if we don’t want to ‘override’ our Methods and Variables in Java?

It’s simple…

Declare them using the keyword ‘final’ as modifier. That’s it.

eg:

final int myVariable = 79;

So the value of myVariable can never be changed any way.

Also for Classes/Methods.

eg:

final class myClass{ whatever your code;} 

It will prevent our myClass being extended.