Showing posts with label two. Show all posts
Showing posts with label two. Show all posts

Wednesday, 7 August 2013

How to generate a random number in corona SDK

We can use math.random() for the purpose of generating random numbers in corona SDK.
There are 3 main types for this call. They are:

1) Witout any argument:
        math.random()
        -- This will return a pseudo random number in the range of [0,1)
   
2) With one argument:
        math.random(a)
        -- This will return a pseudo random number in the range of [0,a]
   
        eg: print(math.random(5))  ----> return a number between 1 and 5 (both inclusive)
   
3) With two arguments:
        math.random(a,b)
        -- This will return a pseudo random number in the range of [a,b]
   
        eg: print(math.random(2,10))  ----> return a number between 2 and 10 (both inclusive)

Monday, 10 June 2013

How to draw simple lines in cocos2d

Here, I will show you 2 simple methods to draw lines in cocos2d applications.



Method 1 - Draw line - default method, updates in each frame:

Syntax:
 
    - (void)draw{
           [super draw];
           glColor4ub(0,0,0,255);
           glLineWidth(2);
           glColor4f(1.0, 1.0, 0.5, 1);
           ccDrawLine(ccp(0,100), ccp(320,150));    // Draw line connecting (0,100) & (320,150)
    }
       

Method 2 - Draw line using CCRibbon:
        This  is a very simple method to draw lines in cocos2d. The advantage of this method is that, you can even draw lines with your textured image.


Syntax:
 
        ccColor4B myColor = ccc4(255, 255, 255, 150);
        CCRibbon *ribbon = [CCRibbon ribbonWithWidth:0.5
                                                                              image:@"green.png"
                                                                              length:1.0
                                                                              color:myColor
                                                                              fade:0.7f];

        [self addChild:ribbon z:8];
       
        [ribbon addPointAt:ccp(0,240) width:2];          // This will initialize the starting point at (0,240)
        [ribbon addPointAt:ccp(320,200) width:2];      // This will connect the previous point[(0,240)]
                                                                                   with (320,200) via a straight line