To answer this we need to talk about how electrons move in an atom. Electrons are in energy levels (orbitals) and an electron in the highest unoccupied orbital can be excited to a higher shell when given enough thermal energy (heat) from the flame (must make sure you refer back to the specifics of a question). This electron then drops back down to its ground state (the shell it was previously in), and this causes a photon to be emitted with energy equivalent to the difference in energies between the two states/orbitals. Different metal ions have different sized gaps between their orbitals (energy levels) and so the energy of the photon emitted will change, hence the wavelength of the photon ( E=hc/f although this is beyond the scope of the syllabus it might be useful to teach some students also studying physics as it would give them a better understanding of the atom and its implications).