Notice it does not use a picking process - the code iterates every Sprite instance, and inside the loop it uses an 'if' statement to check if it should move the sprite.
On collision
The On collision event actually does quite a lot of work. Is overlapping is continually true while instances intersect. However On collision remembers if the instances were previously intersecting, and is only true the first time they intersect, which acts as a trigger for when two instances first touch.
In Construct's JavaScript API, there is only a testOverlap method, which is essentially the equivalent of Is overlapping. There is no equivalent to On collision: the part that remembers the previous state is part of the event system.
Sometimes testing overlap is sufficient, such as if the objects are destroyed on touching, as they won't keep touching. However when coding in JavaScript, it's up to your code to remember if two instances were previously overlapping and do something different the first time it happens. This highlights how event sheets do a lot of extra work for you behind the scenes to make your life easier than the equivalent in a programming language. Often tasks that are simple in event sheets take a lot more work as code.
Some advice
Here are a few extra bits advice about programming to help you along the way.
- Programming often requires patience. When things don't work, try to adopt an attitude of being curious to find out what's going on. While it can be an understandable reaction, try to avoid getting angry or frustrated - it will only make it harder to solve the problem. If you need to, take some time out. Often taking a break, or coming back the next day, is what it takes to solve a problem.
- If you're serious about learning to code well, then try to practice a lot. Writing a lot of different kinds of code to solve different problems is a great way to learn.
- JavaScript is an old language, having originally been created in 1995. Some older parts of it have been superseded by newer, better features, to the extent there's no reason to use the old features at all any more. One good example of this is how
let
has replaced var
for declaring variables. There's loads of information about JavaScript on the web, but when finding resources check the date it was published - the older it is, the more likely that it would be written differently now. There's also no need to learn about how the old legacy features work unless you have to work with some old code that uses it.
- JavaScript is also a constantly improving language, gaining new features regularly. However new features don't always arrive in all browsers or platforms at the same time. Check what the browser support for new features is, so you don't accidentally write code that doesn't work in other browsers. Testing your code works in all major browser engines (Chrome, Firefox and Safari) is also a good way to check that.
- It's great to feel a sense of accomplishment when you write some complicated code that works. However remember that for any given task, the best code is as simple and easy to read as possible while still getting the job done. Good programmers avoid obscure tricks or unnecessary complexity wherever possible.
Closing remarks
This guide has hopefully given you a solid grounding in the JavaScript programming language, while pointing you towards further resources to deepen your understanding of JavaScript. Construct provides a great place to learn to code, allowing you to mix and match event blocks and JavaScript code to help ease your transition to coding, as well as providing an editor and APIs to handle many otherwise tedious tasks of game programming such as level design and rendering.
The JavaScript programming language, and all the APIs available in browsers and tools like Construct, represent a huge amount to learn about. Programming is also often difficult and can be a time-consuming process to get your code right. Finishing this guide is only the very beginning of your journey in to the world of programming. If you're serious about getting in to programming, recognise that like many fields of work, it will probably take a year or more of regular practice to become proficient, and many years further to become an expert. However even with basic skills you can get useful and interesting things done - and you'll be learning skills in an industry-standard programming language, which makes what you learn highly transferable to other JavaScript-based tools and frameworks. And if you go further, JavaScript is the kind of language you can get a job working with!
Congratulations for finishing this guide, good luck with your future efforts and happy coding! 🎉