You don't have access to this class

Keep learning! Join and start boosting your career

Aprovecha el precio especial y haz tu profesi贸n a prueba de IA

Antes: $249

Currency
$209
Suscr铆bete

Termina en:

2 D铆as
11 Hrs
41 Min
55 Seg

Comprobar 3 en raya

17/31
Resources

How to detect candies online to create matches?

Inline candy detection is key to achieve matches in games like Candy Crush. The goal is to identify when three or more candies are aligned in rows or columns. This task falls to the manager, which examines a data matrix looking for matches of three or more candies.

This concept is essential in the classic implementation of Candy Crush, where aligned candies are destroyed. However, it is possible to modify the method to search for other types of matches, such as diagonally or by forming squares of four candies. This will require adjustments to much of the gameplay and code. Below, we explore a basic method for identifying these matches.

How to set up constants and methods to detect matches?

First, you need a constant that defines the minimum number of candies for a match. It is recommended to initialize it starting with a capital letter, following C# best practices. Although the standard could be three candies, it is conceptualized as two neighbors plus the current candy, since in the end, the goal is to generate a match of at least three candies.

How to implement the findMatch method?

We start by creating a method to check for a match in rows or columns. It is built inside the Board Manager class and its purpose is to check if there is a match of three or more candies at a given address. It uses an empty list of game objects(GameObjects) that will store the matching candies.

private List<GameObject> FindMatch(Vector2 direction){ List<GameObject> matchingCandies = new List<GameObject>(); // Logic for finding matching candies to be implemented here}

How to extend the method to candies?

The next step is to program, in the Candy.cs script, a method that looks for neighbors at the given address, either row or column. This method makes sure that the minimum number of matches is achieved to destroy candy.

private List<GameObject> FindMatch(Vector2 direction){ List<GameObject> matchingCandies = new List<GameObject>(); RaycastHit2D hit = Physics2D.Raycast(this.transform.position, direction);
 while (hit.collider != null && hit.collider.GetComponent<SpriteRenderer>().sprite == this.GetComponent<SpriteRenderer>().sprite) { matchingCandies.Add(hit.collider.gameObject); hit = Physics2D.Raycast(hit.collider.transform.position, direction); } return matchingCandies;}

This approach checks for neighbors in the specified direction. If found, it continues until the conditions are met.

How to check in both directions?

To complete the method, it is crucial to evaluate in the opposite direction. This requires duplicating the previous code block and modifying the direction inversely.

// Original codehit = Physics2D.Raycast(hit.collider.transform.position, -direction);
while (hit.collider != null && hit.collider.GetComponent<SpriteRenderer>().sprite == this.GetComponent<SpriteRenderer>().sprite){ matchingCandies.Add(hit.collider.gameObject); hit = Physics2D.Raycast(hit.collider.transform.position, -direction);}

Once the two directions are verified, matching candies are returned. This approach ensures correct neighbor detection in any game situation, whether at the edge or in the center of the board.

This method encourages continuous development and personal improvement, adapting and customizing according to the needs of the game. The logic presented can be the starting point for implementing more advanced features in similar video games. Constant practice and experimentation lead to a deeper mastery of video game programming.

Contributions 8

Questions 1

Sort by:

Want to see more contributions, questions and answers from the community?

馃檲 Darle nombres correctos a nuestras variables puede ser dif铆cil pero, el esfuerzo vale la pena para nosotros y para todo nuestro equipo. 馃 Estas lecturas me ayudaron bastante para programar en JavaScript:

Tambien pueden compararlos con el Id.

 while (hit.collider != null && hit.collider.GetComponent<Candy>().id == id)
        {
            matchingCandies.Add(hit.collider.gameObject);
            hit = Physics2D.Raycast(hit.collider.transform.position, direction);
        }

Super interesante.

EXELENTE

Genial

Todo excelente solo no se cual linea es la que mide en vertical y cual en horizontal,

驴Que paso con el metodo de BoardManager que no termino de escribir?

Hola, comunidad. Aqu铆 hablan de la posibilidad de hacerlo en diagonal, lo cual parece muy interesante. Sin embargo, no tengo idea de c贸mo hacerlo teniendo en cuenta los ejes bidimensionales X y Y. No obstante, si me dan una peque帽a idea para empezar a experimentar diversas cosas para terminar programando esto, podr茅 entender c贸mo est谩n dise帽ados algunos videojuegos y p谩ginas web como Chess.com. 隆Gracias de antemano!