예제 #1
0
    public override void OnAwake()
    {
        CreateBackground();

        //make the start button and add it to the stage
        CinchSprite startButton = CinchSprite.NewFromImage("StartButton");

        this.AddChild(startButton);
        startButton.Width  = 3f;
        startButton.ScaleY = startButton.ScaleX;

        //give it a jaunty little tilt
        startButton.Rotation = 10f;

        //do an animation effect and start the game when startButton is pressed
        startButton.AddEventListener <MouseEvent>(MouseEvent.MOUSE_DOWN, (mouseEvent) => {
            new Tween(startButton, "ScaleX",
                      1.5f, .5f, Easing.Bounce.EaseOut, 0f);
        });

        //add an instructions text field
        SimpleTextField instructions = SimpleTextField.NewFromString("Press Start to begin!", "FjallaOne-Regular", .4f, TextAnchor.MiddleCenter);

        this.AddChildAt(instructions, 1);

        startButton.X  = ViewportWidth / 2;
        startButton.Y  = ViewportHeight / 2;
        instructions.X = startButton.X;
        instructions.Y = startButton.Y - 1.4f;
    }
예제 #2
0
    public override void OnAwake()
    {
        CreateBackground();

        //Cinch2D uses meters rather than pixels.  This makes it much easier to code for devices with varying screen sizes.
        //Sprites come from Textures, which are measured in pixels.
        //When making a new Sprite, use PixelsPerMeter to determine how big it will be in meters.

        //Strawberry texture is 512x512 pixels, so at 256 pixels per meter it will be 2x2 meters.
        var strawberry = CinchSprite.NewFromImage("Cinch2D/Strawberry", 256);

        AddChild(strawberry);
        //Sprites default to 0,0 which is the center of the screen.  This will set it halfway between center and the left edge of the screen
        strawberry.X = ViewportWidth / -4;

        //Registration points are the center point of the Sprite.  The Sprite will move, rotate, and scale around this point.
        //Let's center the watermelon around its bottom-left corner:
        var watermelon = CinchSprite.NewFromImage("Cinch2D/Watermelon", 256, RegistrationPoint.BottomLeft);

        AddChild(watermelon);
        //set its center halfway between center and the right edge.
        watermelon.X = ViewportWidth / 4;

        //you can extend the Sprite class just like in Flash.
        //SmartCherry is a Sprite subclass that rotates continuously.
        //Instantiate Sprite subclasses via Library.New<Subclass Type>(new name);
        _smartCherry = Library.New <SmartCherry>("SmartCherryInstance");
        AddChild(_smartCherry);

        //Sprites can be added to each other.  This handler will add whatever was clicked to SmartCherry's display tree
        watermelon.AddEventListener <MouseEvent>(MouseEvent.MOUSE_DOWN, AddToSmartCherry);
        strawberry.AddEventListener <MouseEvent>(MouseEvent.MOUSE_DOWN, AddToSmartCherry);
    }
예제 #3
0
    //don't use Start(), Awake(), Update(), or LateUpdate().
    //Use OnAwake(), OnEnterFrame(), OnExitFrame() instead
    public override void OnAwake()
    {
        //ViewportWidth and ViewportHeight will give you the dimensions of the viewport
        //Let's figure out if we're running in landscape or portrait:
        bool isLandscape = ViewportWidth > ViewportHeight;

        //you can also set ViewportWidth or ViewportHeight.  Since aspect ratio is fixed, setting one will change the other.
        //Let's set the larger dimension to 20 meters:
        if (isLandscape)
        {
            ViewportWidth = 20f;
        }
        else
        {
            ViewportHeight = 20f;
        }

        //the origin (0,0 point) is in the middle of the screen.  Anything we add to the stage will show up there by default.
        //Let's add a strawberry to the screen:
        var strawberry = CinchSprite.NewFromImage("Cinch2D/Strawberry");

        AddChild(strawberry);

        //Stage also has Width and Height properties.  These are different from ViewportWidth/Height.
        //They are the width/height of the contents of the Stage, rather than the Stage itself.
        //Let's see how big the strawberry is:
        Debug.Log("Stage contents are " + Width + "x" + Height + " meters");
    }
예제 #4
0
    private void VerifySizingForEmptyParent()
    {
        var emptyParent = Library.New <DisplayObjectContainer>("EmptyParent");

        AddChild(emptyParent);
        emptyParent.AddChild(CinchSprite.NewFromImage("Earth", 158f, RegistrationPoint.Center));

        //should be able to set width/height, scaleX/Y to whatever without any issues
        emptyParent.Width  = 20;
        emptyParent.Height = 30;
        emptyParent.ScaleX = 2;
        emptyParent.ScaleY = 10;

        var caughtSmallWidthException  = false;
        var caughtSmallHeightException = false;
        var caughtLargeWidthException  = true;
        var caughtLargeHeightException = true;

        try     { emptyParent.Width = 0; } catch (Exception) { caughtSmallWidthException = true; }
        try     { emptyParent.Height = 0; } catch (Exception) { caughtSmallHeightException = true; }
        try     { emptyParent.Width = 10000f; } catch (Exception) { caughtLargeWidthException = true; }
        try     { emptyParent.Height = 10000f; } catch (Exception) { caughtLargeHeightException = true; }

        if (!caughtSmallWidthException || !caughtSmallHeightException)
        {
            throw new Exception("Test failed for throwing on small sizes");
        }

        if (!caughtLargeWidthException || !caughtLargeHeightException)
        {
            throw new Exception("Test failed for throwing on large sizes");
        }

        RemoveChild(emptyParent);
    }
예제 #5
0
    public override void OnAwake()
    {
        CreateBackground();

        //Create a container for "GamePlay" (a bunch of floating berries)
        _gameplayContainer = Library.New <CinchSprite>("StrawberriesContainer");
        AddChild(_gameplayContainer);

        //pausing this Clock will pause all children
        _gameplayContainer.Clock = new GameClock("GameplayClock");

        //Create 15 FloatingStrawberry's
        for (var i = 0; i < 15; i++)
        {
            _gameplayContainer.AddChild(Library.New <FloatingStrawberry>("Berry" + Random.Range(1, 1000)));
        }

        //Create a button to pause gameplay
        _pause = CinchSprite.NewFromImage("Cinch2D/Pause");
        AddChild(_pause);
        //SetPosition is faster than setting X and Y individually, since X and Y each cause display chain updates
        _pause.SetPosition(ViewportWidth / 2 - _pause.Width, ViewportHeight / 2 - _pause.Height);
        _pause.AddEventListener <MouseEvent>(MouseEvent.MOUSE_DOWN, Pause);

        //Create a button to start gameplay
        _play = CinchSprite.NewFromImage("Cinch2D/Play");
        AddChild(_play);
        _play.SetPosition(ViewportWidth / 2 - _play.Width, ViewportHeight / 2 - _play.Height);
        _play.AddEventListener <MouseEvent>(MouseEvent.MOUSE_DOWN, Play);
        _play.Visible = false;
    }
예제 #6
0
    // Use this for initialization
    public override void OnAwake()
    {
        CinchOptions.DefaultPixelsPerMeter = 25f;

        Stage.Instance.TheCamera.backgroundColor = new Color(.2f, .2f, .2f, 1f);
        _joesContainer = Library.New <CinchSprite>("Joes Container");
        AddChild(_joesContainer);
        _joesContainer.X     = Stage.Instance.ViewportWidth / 2;
        _joesContainer.Y     = Stage.Instance.ViewportHeight / 2;
        _joesContainer.Clock = new GameClock("JoesContainerClock");

        _play  = CinchSprite.NewFromImage("Play", 100f);
        _pause = CinchSprite.NewFromImage("Pause", 100f);
        AddChild(_play);
        AddChild(_pause);
        _play.Visible = false;
        _play.X       = _pause.X = ViewportWidth - .5f;
        _play.Y       = _pause.Y = ViewportHeight - .5f;

        _play.AddEventListener <MouseEvent>(MouseEvent.MOUSE_DOWN, onPlayPress);
        _pause.AddEventListener <MouseEvent>(MouseEvent.MOUSE_DOWN, onPausePress);

        _joes = new List <CinchSprite>();
        for (var i = 0; i < 15; i++)
        {
            var joe = CreateJoe();
            _joes.Add(joe);
            joe.ScaleX = -1;
        }
    }
예제 #7
0
    public override void OnAwake()
    {
        CreateBackground();

        _physicsContainer = PhysicsContainer.NewEmpty(Vector2.up * -30f);
        AddChild(_physicsContainer);

        var field = CinchSprite.NewFromImage("Cinch2D/Field", 512);

        field.Width  = ViewportWidth;
        field.Height = 1f;

        //create a physics body for the field, same size as the CinchSprite
        var fieldBody = BodyFactory.CreateRectangle(_physicsContainer.World, field.Width, field.Height, 1);

        //as soon as we set the fieldBody as field's PhysicsBody, field will take its position and rotation from fieldBody after every frame
        field.PhysicsBody = fieldBody;
        //align it with the bottom of the screen
        //setting physics body position will update sprite's position on next frame
        fieldBody.Position = new Vector2(0, 0 - ViewportHeight / 2 + field.Height / 2f);
        //set the field as static so it uses less CPU
        fieldBody.IsStatic = true;
        //make it a wee bit bouncy
        fieldBody.Restitution = .5f;
        _physicsContainer.AddChild(field);

        //create a soccer ball to roll around
        var ball = CinchSprite.NewFromImage("Cinch2D/SoccerBall", 256);

        _ballBody          = BodyFactory.CreateCircle(_physicsContainer.World, .5f, 1f);
        ball.PhysicsBody   = _ballBody;
        _ballBody.BodyType = BodyType.Dynamic;
        //drop it on the right side of the screen
        _ballBody.Position = new Vector2(ViewportWidth / 2 - 1, 0);

        //we add all physics objects to the same parent.
        //Adding a physics object as a child of another physics object (like a wheel as a child of a car), will produce super-bad results.
        //you can still add non-physics objects to different containers, like a mud splat as a child of the ball
        _physicsContainer.AddChild(ball);

        var player1 = CreatePlayer();
        var player2 = CreatePlayer();

        _physicsContainer.AddChild(player1);
        _physicsContainer.AddChild(player2);

        player1.Y = player2.Y = 0 - ViewportHeight / 2 + field.Height + player1.Height / 2;
        player1.X = 0 - ViewportWidth / 2 + 1;
        player2.X = ViewportWidth / 2 - 1;
        player1.PhysicsBody.OnCollision += BallHitPlayer1;
        player2.PhysicsBody.OnCollision += BallHitPlayer2;

        _debugDrawButton      = Library.New <TextButton>("DebugDrawToggle");
        _debugDrawButton.Text = "Debug: Off";
        AddChild(_debugDrawButton);
        _debugDrawButton.Y = 0 + ViewportHeight / 2 - _debugDrawButton.Height;
        //toggle debug draw when pressed
        _debugDrawButton.AddEventListener <MouseEvent>(MouseEvent.MOUSE_UP, ToggleDebugDraw);
    }
예제 #8
0
    private void CreateBackground()
    {
        CinchSprite background = CinchSprite.NewFromImage("Background", 100f, RegistrationPoint.BottomLeft);

        background.Width  = ViewportWidth;
        background.Height = ViewportHeight;
        AddChild(background);
    }
예제 #9
0
    protected void CreateBackground()
    {
        CinchSprite background = CinchSprite.NewFromImage("Cinch2D/Background", 100f);

        background.Width  = ViewportWidth;
        background.Height = ViewportHeight;
        AddChild(background);
    }
예제 #10
0
    public override void OnAwake()
    {
        base.OnAwake();

        VerifySizingForEmptyObject();
        VerifySizingForEmptyParent();

        if (ViewportWidth > ViewportHeight)
        {
            ViewportWidth = 10f;
        }
        else
        {
            ViewportHeight = 10f;
        }

        _sizeChart      = CinchSprite.NewFromImage("SizeChart", 100f, RegistrationPoint.BottomLeft);
        _sizeChart.Name = "SizeChart";
        _sizeChart.X    = ViewportWidth / -2;
        _sizeChart.Y    = ViewportHeight / -2;
        AddChild(_sizeChart);

        //give it a weird pixels-per-meter to make sure it's not an even width in meters
        _earthContainer = Library.New <DisplayObjectContainer>("EarthContainer");
        _earthContainer.SetPosition(3, 7);
        AddChild(_earthContainer);
        var innerContainer = Library.New <DisplayObjectContainer>("InnerContainer");

        _earthContainer.AddChild(innerContainer);
        var yetAnotherContainer = Library.New <DisplayObjectContainer>("YetAnotherContainer");

        innerContainer.AddChild(yetAnotherContainer);
        var earth = CinchSprite.NewFromImage("Earth", 158f, RegistrationPoint.Center);

        earth.MouseEnabled = true;
        earth.Name         = "Earth";
        earth.AddEventListener <MouseEvent>(MouseEvent.MOUSE_DOWN, onEarthPress);
        yetAnotherContainer.AddChild(earth);

        //give it a weird pixels-per-meter to make sure it's not an even width in meters
        _mars = CinchSprite.NewFromImage("Mars", 212f, RegistrationPoint.BottomLeft);
        _mars.MouseEnabled = true;
        _mars.Name         = "Mars";
        _mars.SetPosition(.5f, .5f);
        _mars.AddEventListener <MouseEvent>(MouseEvent.MOUSE_DOWN, onMarsPress);
        AddChild(_mars);
    }
예제 #11
0
    public override void OnAwake()
    {
        base.OnAwake();
        CreateBackground();

        //create a watermelon to listen to.
        var watermelon = CinchSprite.NewFromImage("Cinch2D/Watermelon", 256);

        AddChild(watermelon);
        watermelon.Name = "Watermelon";

        //let's add a label to the melon
        var textField = SimpleTextField.NewFromString("Watermelon!", "Cinch2D/FjallaOne-Regular", .5f);

        watermelon.AddChild(textField);
        textField.Name = "TextField";

        //now add a listener.  Cinch2D implements MOUSE_DOWN, MOUSE_UP, RELEASE_OUTSIDE, MOUSE_OVER, MOUSE_OUT, and MOUSE_MOVE
        watermelon.AddEventListener <MouseEvent>(MouseEvent.MOUSE_DOWN, onWatermelonPress);
    }
예제 #12
0
    public override void OnAwake()
    {
        var panel = CinchSprite.NewFromImage("Cinch2D/DialogBackground", 256f);

        //leave a 20% border around the edge of the panel, that won't scale
        panel.SetScale9Grid(new Rect(.2f, .2f, .6f, .6f));
        AddChild(panel);

        panel.AddEventListener <MouseEvent>(MouseEvent.MOUSE_DOWN, (e) => {
            panel.ScaleX = .1f;
            panel.ScaleY = 2f;
            new Tween(panel, "ScaleX", 1f, 2f, Easing.Bounce.EaseOut);
            new Tween(panel, "ScaleY", 1f, 2f, Easing.Bounce.EaseOut);
        });

        var instructions = SimpleTextField.NewFromString("Click panel", "Cinch2D/FjallaOne-Regular", .5f);

        AddChild(instructions);
        instructions.Y = -1.5f;
    }
예제 #13
0
    public override void OnAwake()
    {
        CreateBackground();

        //4 ways to create Sprites:

        //1: Create empty Sprites with Sprite.NewEmpty() or Library.New<Sprite>()
        var container = CinchSprite.NewEmpty("Container");

        this.AddChild(container);
        container.AddEventListener <MouseEvent>(MouseEvent.MOUSE_DOWN, DestroyWhateverWasClicked);

        //2: Create from Texture via Sprite.NewFromImage()
        var cherries = CinchSprite.NewFromImage("Cinch2D/Cherries", 256);

        container.AddChild(cherries);
        cherries.X = ViewportWidth / -4;

        //3: Library.New<Any CinchSprite Subclass>().
        //Watermelon is defined in a separate file, and actually contains another way to instantiate a sprite
        var watermelon = Library.New <Watermelon>("Watermelon");

        container.AddChild(watermelon);

        //4: Library.New("SymbolId")
        //first, add a definition.  You can define all your symbols in one place using this method.
        var strawberryDef = new SymbolDef {
            SymbolId          = "Strawberry",
            TexturePath       = "Cinch2D/Strawberry",
            PixelsPerMeter    = 256,
            RegistrationPoint = RegistrationPoint.Center
        };

        Library.AddDefinition(strawberryDef);

        //then instantiate the definition.
        var strawberry = Library.New("Strawberry");

        container.AddChild(strawberry);
        strawberry.X = ViewportWidth / 4;
    }
예제 #14
0
    public override void OnAwake()
    {
        CreateBackground();
        CinchOptions.DefaultPixelsPerMeter = 180;

        //make a container clip at the lower left corner
        CinchSprite lastParent = Library.New <CinchSprite>("root");

        AddChild(lastParent);
        lastParent.Rotation = 0f;

        for (var i = 0; i < 20; i++)
        {
            var newSprite = CinchSprite.NewFromImage("Cinch2D/Strawberry");
            lastParent.AddChild(newSprite);
            newSprite.X        = .6f;
            newSprite.Rotation = 12f;
            newSprite.ScaleX   = newSprite.ScaleY = .95f;

            lastParent = newSprite;
        }
    }